JavaScript - Pattern Match And Condition Check
I am trying to do a pattern match and check something in a condition but cant get it to work.
The first value changes and I need to check and do something if I get a hit on it. Code: var mydata = "?first=one&second=two&third=three"; if (mydata.indexOf("first") == "something") { alert("No Hit"); } else { alert("Hit"); } Basically I am trying to find out if first is equal to one in the mydata string. Please advise because my above attempt is not working. Similar TutorialsHi All, Following pattern in bold is present in a variable bigvar which has around 100 characters. Variable test may vary var test = CONFIG_ID CONFIG_ID="_N/D1/N1/S4/P1/E1%231.1.1/T 3" Here is what I have tried: var RE = new RegExp (test+"=[A-Za-z0-9_%\"\.\/]+\s{2}[0-9]+"); var matched_val = bigvar.match(RE); alert(matched_val); It is giving null as match. Can anyone help me on this ? Thanks, Vinay I have the code for the page below that has form with a text box for a email address. I am doing this exercise trying to understand the concepts of pattern matching in a function. ***My expression on needs to tests for *** 1. One or more word characters= /\w+ 2. one period= \.\ 3. one hyphen sign= \-\ 4. two or more characters that a-z, A-Z, 0-9, period or hyphen= \W+\. I have a alert message that display when the user has returned a invalid address. I don't need regex for testing an entire email address that is out of the scope that I am studying. I have wrote a function but to honest I am not sure where I have gone wrong and not exactly where I need to go to execute it correctly. <!DOCTYPE html> <html> <head> <title>Lab 2 Part 3</title> <meta charset="UTF-8" /> <body> <script> function validation(address) { var ok= address.search(/\w+\.\-\@\W+); if (ok==0) return true; else alert("please enter a valid email address"!!) return false; } </script> <form action="http://yahoo.com/formtest.php" method="post" onsubmit="return validation();"> <p> <label> Email Address: <input type="text" name="address" size="30"/> </label> </p> <input type="submit" value="Submit" /> </form> </body> </html> Hi, I would like to know which is the best approach when trying inheritance with js - module pattern. In the example below, I want that parent.hi() be fired from child: Code: var parent = function(){ return{ hi: function(){ console.info('hi'); } } }(); var child = function(){ return{ bye: function(){ console.info('bye'); }, hi: function(){//look he parent.hi(); } } }(); child.hi(); Is there a better way to do this ? Thanks in advance HI I have seen this pattern in http://www.altsoftware.com/index.php . there are news menu in the left side . Please visit this site . The news will be change with a really beautiful pattern in every 5 seconds I think it has been written with jquery but I don't know how can I make something like this How can I do so ? thanks Hi, I want to validate the value in a textbox such that the first character should be a symbol like # or $ followed by 2 alphabets which is followed by numbers.I tried a pattern like if(/(#{1}|${1})\D{2}\d{6}/.test(str.value)) But this is not working. Can someone help me the correct pattern? I am new to javascript and would like to search for the element with id but the element id are changing if i use different navigation in that web page example: C21_W68_V69_notes_struct.text changes to C24_W78_V79_notes_struct.text or any other name next time hence i would like to search that element using a pattern like notes_struct.text as there exist only one element ending with this pattern. I am using old version IE and would like to use javascript only. Kindly help. The "?" seems to be called the conditional operator. Code: var points = prompt('How many points do you want on ten?'); var result = (points >= 5) ? "succeeded!" : "didn't succeed ;-)"; window.alert("You " + result); i have been searching the net to find a way to condition ondragover results by file type being dragged. Code: function doDragOver(event) { if(event.dataTransfer.types.contains('image/png')){ event.preventDefault(); } if(event.dataTransfer.types.contains('image/gif')){ event.preventDefault(); } if(event.dataTransfer.types.contains('image/jpg')){ event.preventDefault(); } if(event.dataTransfer.types.contains('image/jpeg')){ event.preventDefault(); } } this is waht i have been trying based on the ondragover documentation on the MDN network. i need preventDefault to be triggered if what i am dragging is an image from thee users hard drive and not triggered if the element is dragged from the browser. i can find no listing of type formats or even discovering file types for the dragged item on any network. I would greatly appreciate any help. ps. i have been stuck on this for three days now, i may be losing my mind I have an table in which an select box like this OS:<td > <select name="os" id="os" class="SelectList Width200"> </select> </td> following have an option in OS: windows/Linux i only want when user select OS - linux ... then function call otherwise if user select OS - window ... no need to call this function validate() i think it requires if condition in function Validate() .... can u ans me how it is posible to put if condition in function validate() function Validate() { var dropdownIndex = document.getElementById('type').selectedIndex; var sztype = document.getElementById('type')[dropdownIndex].value; if(sztype == "1") { var szpassword = ""; var szguid = ""; var name = Trim(document.userform.name.value); var ip = Trim(document.userform.ip.value); if(ip.length == 0) { alert("Please specify IP/Host Name."); return false; } if(name.length == 0) { name=ip; } szusername = Trim(document.userform.username.value); if(szusername.length == 0) { alert("Please specify Username."); return false; } var szvalidchars = new RegExp("[~!@#$%^&*()+|{}:\"<>?,/;'=\\`]"); if(szvalidchars.test(szusername)) { alert("Username cannot contain invalid characters."); return false; } szpassword = Trim(document.userform.password.value); if(szpassword.length == 0) { alert("Please specify Password."); return false; } Could anyone tell me (or link me to some place that can) why the following if-statement will execute? if (5!=6 || 7!=8) { alert("Why does this work?"); } Thanks Hi to All, I have two menus, related each other, in this way: <head> var sum_db = new Object() sum_db["email1"] = [{value:"I01 - Problems of one level", text:"I01 - Problems of one level"}, {value:"I02 - Problems RDM", text:"I02 - Problems RDM"}, {value:"Free Text", text:"Free Text "}]; sum_db["email2"] = [{value:"I03 - Various", text:"I03 - Various"}]; // // function setIssue(chooser) { var newElem; var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; var Iss_Chooser = chooser.form.elements["iss_sum"]; while (Iss_Chooser.options.length) { Iss_Chooser.remove(0); } var choice = chooser.options[chooser.selectedIndex].value; var db = sum_db[choice]; if (choice == "email1") { newElem = document.createElement("option"); newElem.text = "Choose an issue ..."; newElem.value = "Choose Issue ..."; Iss_Chooser.add(newElem, where); } for (var i = 0; i < db.length; i++) { newElem = document.createElement("option"); newElem.text = db[i].text; newElem.value = db[i].value; Iss_Chooser.add(newElem, where); if ((choice == "email1") && (db[i].text.indexOf('Free Text')==0)) { document.getElementById("label_des").style.display=""; } else { document.getElementById("label_des").style.display="none"; } } } // </script> </head> <body> <form> <select size="1" name="mess" id="mess" onchange="setIssue(this)"> <option value="email1" selected>email1</option> <option value="email2">email2</option> </select> .... .... <select id="iss_sum" name="iss_sum"> <option value="<%=objRS("Issue_Summary")%>" selected><%=objRS("Issue_Summary")%></option> </select> <label for="iss_des" id="label_des" style="display:none; vertical-align:top">Description:<textarea id="iss_des" name="iss_des"><%=objRS("Issue_Description")%></textarea> </label> My problem is: I would like that if I select ONLY email1 and ONLY the option "Free Text", appears the textarea with label Description. Up to know if one selects email1, automatically the textarea appears near for every option; it seems that the condition of the if is not respected !!! Thanks a lot in advance !!! Upon clicking a button, my script will send a request to a PHP file, which will create a database entry and return it to the script, using json. Involving what's returned (which is an integer), javascript will create new div elements with that integer in the element blocks. My fear is that when the user sends two (or more) requests and there's a difference in ping, or he just has packet loss - that request #2 will come back while request #1 is executing. I could add a forced delay, where the PHP script will not allow a second entry within 5 seconds of the last entry. However, I'm afraid that massive packetloss could still interfere. For example, this could result in 2 div elements having the number of the first request, and 1 element having the number of the second request. If possible, I would like to avoid forcing the user to refresh the page to make the SQL entry (i.e., create the element with javascript and without SQL/PHP, but then upon submission, reload the page and send them to SQL.) Any suggestions? Hi, I have 3 sum fields which I want to change to a red colour if the value is > or < 100. Apparently this is a DAP, but I am using javascript to tweek some of the settings in DAP. function subtotal() if(document.getElementById("Sum1").value>100&&<100) { this.style.cssText='Z-INDEX: 2; LEFT: 6.667in; WIDTH: 0.729in; COLOR: red' { else this.style.cssText='Z-INDEX: 2; LEFT: 6.667in; WIDTH: 0.729in; COLOR: #339966' </SCRIPT> <SPAN class=MSTheme-Label id="Sum1_Label" style="Z-INDEX: 1; LEFT: 0.25in; OVERFLOW: hidden; WIDTH: 0.711in; POSITION: absolute; TOP: 0.125in; HEIGHT: 0.161in">Totals:</SPAN><SPAN class=MsoBoundSpan id="Sum1" onmouseover="subtotal()" dataFormatAs=TEXT style="Z-INDEX: 2; LEFT: 5.416in; WIDTH: 0.729in; COLOR: #339966; POSITION: absolute; TOP: 0.125in; HEIGHT: 0.187in" MsoTextAlign="General">Sum of Product 1</SPAN><SPAN class=MsoBoundSpan id="Sum2" onmouseover="subtotal()" dataFormatAs=TEXT style="Z-INDEX: 2; LEFT: 6.667in; WIDTH: 0.729in; COLOR: #339966; POSITION: absolute; TOP: 0.125in; HEIGHT: 0.187in" MsoTextAlign="General">Sum of Product 2</SPAN><SPAN class=MsoBoundSpan id="Sum3" dataFormatAs=TEXT style="Z-INDEX: 2; LEFT: 7.917in; WIDTH: 0.729in; COLOR: #339966; POSITION: absolute; TOP: 0.125in; HEIGHT: 0.187in" MsoTextAlign="General">Sum of Product 3</SPAN></DIV> I'm a noobie at coding, and I was wondeirng if anyone can help me with the coding. Many Thanks I can't figure this. Basically I have a dialogue pop out box. When you type a value in the pop out box,and press enter, it will change the value in the input boxes. Let say I have five input boxes, four of the input box has 7 as value,the last input box has 15 value. When I type 8 and enter, it should change only the value of 15(from 15 to 8) as 15 is greater than 8. If I type 4, it should change all the values of input boxes as all of the input box values are greater than 4. I have set the condition to 18 as input boxes are 18 columns. jsNewDisc is the user input value. Below are some pieces of codes. Of course if I type 5 and enter, this condition will change all the values in the five input boxes as they are all of greater value. If I input 8, input box that has a value of 15 will be the only one that will be change as it has greater value. Code: for (a=1;a < 18; a++){ if(document.getElementById('R4'+ a).value>jsNewDisc){ document.getElementById('R4'+ a).value = jsNewDisc; } } Currently I have a race condition existing in my Java script code. What I am trying to do with this code is convert all check boxes which are a part of the 'checkbox' class and are not checked into text boxes with a value of zero. Currently when you post a check box that is not checked it does not appear in the $_POST data. However I need to know all the values whether true or false for these particular set of check boxes. The code I have is he Code: function checkboxConvert() { var chkBxs = $$('.checkbox'); for (var i = 0; i < chkBxs.length; i++) { if (chkBxs[i].checked == false) { chkBxs[i].type = 'textbox'; chkBxs[i].value = '0'; } } setTimeout("document.productForm.submit();",1000); } Now the problem that I have been getting is that when I try to submit this form the values of the recently changed text boxes does not appear in the $_POST data. Therefore, as you can see above I have postponed the page submit for 1 sec and then I have all the data available to me. However as time goes on and my data set gets larger, 1 sec may no longer be enough. This I think is a race condition and I need to figure some way of running the code only after all the check boxes have been converted and they have their new values. I would have thought that this would be unnecessary from the start, but for some reason it's trying to run both pieces simultaneously and I can't submit until I have the proper values in place. Any help is much appreciated! Hi, In the attached code the outcome always defaults to the first test, even if the condition is false. How would I alter the syntax so that both condtions are evalauted? Code: var res = document.calc.num1.value*100/document.calc.num2.value; function cost1() { if (isNaN(res)) { document.calc.result1.value=0; } else { document.calc.result1.value=document.calc.num1.value*100/document.calc.num2.value; } } Thanks Iain I have a function to accept number and certain text only when "Enter" key is pressed. Code: function handleEnter(field, event){ var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode; var tmp_val = ""; if (keyCode == 13) { if(field.name=="number" && field.value.replace(/ /g,"").length>1){ field.value = field.value.replace(/ /g,""); field.value = field.value.toUpperCase(); tmp_val = field.value; if (isNaN(tmp_val) && tmp_val!="ABC") { field.value = ""; field.focus(); return false; } else { some codings here } } } } However, when I added a second condition, the code stops working Code: if (isNaN(tmp_val) && (tmp_val!="ABC" ||tmp_val!="DEF") ) Anyone pls help~ Tks~ On this webpage http://www.corkdiscos.com/testimonials.html i have a like button. when a user clicks like a comment box appears. when i unlike the button the comment box disappears this is ok but when a user has already liked the facebook page and comes to my webpage the comment box does not show. so im looking for a piece of javascript to check if a user has like the button on my page and if so to show the comment box. please check my source code of the website http://www.corkdiscos.com/testimonials.html to see what i have so far. any help would be greatly appreciated Hi guys. I'm working a bunch of pre existing code on a CMS. Just after a quick fix. Doing a show/hide thing on a particular div somewhere on the page depending if a checkbox is ticked or not. Currently there is 3 checkboxes that are dynamically added through the CMS. Here's simplified version of the form: Code: <form id="simplesearch" name="simplesearch"> <input type="checkbox" onclick='showhidefield(this.value)' name="meta_data_array_search_criteria[custom_profile_type][]" value="5" class="input-checkboxes" /> <input type="checkbox" onclick='showhidefield(this.value)' name="meta_data_array_search_criteria[custom_profile_type][]" value="4" class="input-checkboxes" /> </form> And here's the javascript I was playing with. Code: function showhidefield(id) { if(document.simplesearch.meta_data_array_search_criteria[custom_profile_type][''].checked) { document.getElementById("profile_fields_wrapper_" + id).style.visibility = "visible"; } else { document.getElementById("profile_fields_wrapper_" + id).style.visibility = "hidden"; } } Problem I'm having is how do i do a check to see if those checkboxes are checked in the javascript with those name arrays? How do i separate them? 'm guessing I have to loop through them or something?Hopefully that make senses - it's late here and I'm losing the plot Any pointers would be gratefully welcomed |