JavaScript - Matching Pattern In Javascript
Hi 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 Similar TutorialsI 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> 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. I'm writing a matching game. I have uploaded an example. I want to be able to read the current image rather than the default image as a variable to compare in an IF statement. http://www.mediafire.com/file/8nq4s5...a/matching.zip This is my first post. I am reading "Javascript The Good Parts" ~ The Method Invocation Pattern Page 28 and tried this, var myObject= { value:0, increment:function(inc){ this.value+= typeof inc==="number" ? inc:1; } } myObject.increment(2); alert(myObject.value); but alert(myObject.value); is returning a value of "2", when it should return "3". Can someone help? Reply With Quote 12-22-2014, 09:59 AM #2 rnd me View Profile View Forum Posts Visit Homepage Senior Coder Join Date Jun 2007 Location Urbana Posts 4,497 Thanks 11 Thanked 603 Times in 583 Posts 0+2=2, not 3; the code is working correctly. 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 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. Hi all, I'm trying to match two passwords to one another. The user enters the password into two fields and onblur from the second field, the function runs. Here is my code: Code: function validatePassword(inputField, inputField2, helpText, helpMessage) { // First see if the input value contains data if (!validateNonEmpty(inputField, helpText)) return false; var inputField = document.getElementById('inputField').firstChild.nodeValue var inputField2 = document.getElementById ('inputField2').firstChild.nodeValue } if (inputField !== inputField2) { // The data is invalid, so set the help message and return false if (helpText != null) helpText.innerHTML = helpMessage; } else { // The data is OK, so clear the help message and return true if (helpText != null) helpText.innerHTML = ""; } }; Code: document.getElementById('password2').onblur = function() { validatePassword(this, document.getElementById('password'), document.getElementById('password2_help'), "<p>Passwords don't match</p>'"); }; I am passing a help text to a span element inside the form: Code: <div class="fieldset"> <label for="password2">Confirm:</label> <input type="password" id="password2" name="password2" /> <span id="password2_help" class="help"></span><br/> </div><!--fieldset--> What's going on here? As far as I can tell it this should be working. Thanks, Andrew So I have created a simple calendar (http://www.akapa.org/newsite/events/calendar.htm) and I have a bunch of "More Info" buttons I made from cells. In a table cell to the right are a bunch of hidden DIV's that all appear when you rollover a "More Info" button, obviously giving more details about the calendar date/event. My problem is that I have to give an exact "top" css position to every one of these hidden div's every single time I add a new event to the calendar. I want to dynamically tell each hidden div to match the "top" css position to match the top position of each "More Info" button. How do I do that? I have an autocomplete script but it's currently returning results in a character is in the name anywhere at all, what I need it to do is only return matches if the characters are in sequence only. Example of how it is now if you type S it will return: Science Host Pass What I need it to do is only return a match if the first character is an S, and so on down the line in order. So if you type sam it should only return matches where the first 3 letter are sam in the order it's typed. Here's how the regexp is now: return value.replace(new RegExp("(?![^&;]+(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+", "gi"), "<strong>$1</strong>"); Here's another problem I'm having, I have to validate credit card types. My code worked originally. I simply checked the prefix for each type then verified the length with an if statement. However, I decided to change the code so that only numbers would work after the prefix (as the old code would have allowed any character). This is the code I have: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Credit Card Validation</title> <link rel="stylesheet" type="text/css" href="default.css" /> <script type="text/javascript"> function checkCC(myForm) { var ccType; var ccLength; var ccNum = myForm.cardNum.value; if (myForm.cardType.selectedIndex == 0) { ccType = "Visa"; } else if (myForm.cardType.selectedIndex == 1) { ccType = "MasterCard" } else if (myForm.cardType.selectedIndex == 2) { ccType = "AmericanExpress" } switch(ccType) { case "Visa": { valid = /^4\d{12}(?:\d{3})?$/; if (valid.test(myForm.cardNum.value)) { alert("This is a Valid Visa Card"); return true; } else { alert("This Card Number is Invalid For Visa"); return false; } break; } case "MasterCard": { valid = /^5[1-5]\d{14}$/; if (valid.test(myForm.cardNum.value)) { alert("This is a Valid MasterCard Card"); } else { alert("This Card Number is Invalid For MasterCard"); } break; } case "AmericanExpress": { valid = /^3[47]\d{13}$/; if (valid.test(myForm.cardNum.value)) { alert("This is a Valid American Express Card"); //return true; } else { alert("This Card Number is Invalid For American Express"); //return false; } break; } default: myForm.cardNum.value = null; alert("Card type not found"); return false; } } </script> </head> <body> <h1>Credit Card Validator</h1> <table> <form name="creditCard"> <tr> <td><span class="labels">Card Type:</span></td> <td> <select name="cardType" size="3"> <option name="visa">Visa</option> <option name="mc">Master Card</option> <option name="amex">American Express</option> </select> </td> <tr> <td><span class="labels">Card Number:</span></td> <td><input name="cardNUm" type="text" size=30 value="" /></td> </tr> <tr><td colspan="2"> </td></tr> <tr> <td> <input type="button" value="Death to All" style="background-color:#666666 !important" onclick="return(checkCC(this.form,))" /> </td> </tr> </form> </table> </body> </html> Upon pressing the button nothing happens, I'm completely unsure where I went wrong as all the code seems valid... Please help. Thanks in advance. Hello! I'm in need to be able to create a matching picture game, in which I have rougly 9 images to be able to match, but 18 in total. I have started with a thought of having a button that; when pressed it will disappear and show the image that is within the function. Progress; I am able to press the button and let the image load, but in this case everything goes fine (image resized, not sure about the id though) but the image doesn't show, it just shows an [X] yet when I just put Code: <script language="Javascript" type="text/javascript"> function callImage(){ document.write("<img src=\"images/truck0.jpg">") } </script> The function works fine when I press the button. Any tips? Thank you! Full Code so Far: Code: <head> <script language="Javascript" type="text/javascript"> function callImage(){ document.write("<img src=\"images/truck0.jpg id=\"pic\" width=\"150\" height=\"150\">") } </script> </head> <body> <form> <input type="button" value="Display" onClick="callImage()"/> </form> </body> Hi Having a small problem with writing out the matches from an array using a For loop. I have two arrays, lets say arrayA and arrayB. In arrayB are numbers which are a number of miles, ie 1,2,6,4,5,6,6. And in arrayA are the days of the week. Each day of the week is associated with a mileage, ie Mon = 1, Tues = 2 etc. My script has found the largest mileage in arrayB. Next I have to find the days of the week that match this highest mileage and write these out, along the lines of "The highest mileage was 6 run on Wed, Sat, Sun." I have managed to get a For loop to work with this BUT..... I can only get it to write out the first instance of the day the match is found. ie "The highest mileage was 6 run on Wed," Pointers in the right direction to help me solve this problem would be much appreciated. [CODE] maximumDistanceIndex = 0; for (var distance = 1; distance < distanceArray.length; distance = distance + 1) { if (distanceArray[distance] > distanceArray[maximumDistanceIndex] ) { maximumDistanceIndex = distance document.write ('The maximum distance was ' + maximumDistance + ' km' + ' run on ' + dayArray[maximumDistanceIndex] ); } } [CODE] Hi I am currently writing a program to analyse (to a degree), the points scored in a dance contest. The program mainly works so far. However I am struggling to conclude the program end. Using an if statement, I would like to compute if a dance-off is required, looping through the combinedPointsArray and determining if 2 or more numbers hold the higest score and are equal. The aim is to provide: Maximum number of combined points The couples and their combined points The couples names with the highest points If two or more couples have equal highest combined points - output whether a dance-off is required. This is my program so far: Code: <HTML> <HEAD> <LINK REL="shortcut icon" TYPE="image/x-icon" HREF="favicon.ico"> <TITLE> Latin Dancing Contest </TITLE> <SCRIPT LANGUAGE = "JavaScript"> var contestantNamesArray = ['Tom and Nazia', 'Pat and Dan', 'Sandra and Kofi', 'Ian and Adele', 'Paul and Costas']; var judgesPointsArray = [2,1,5,4,3]; var audiencePointsArray = [4,5,2,3,1]; var combinedPointsArray = new Array (judgesPointsArray.length) for (x=0;x<combinedPointsArray.length;x++) { combinedPointsArray[x] = parseInt( judgesPointsArray[x] + audiencePointsArray[x] ); } var biggest = 0; var values = new Array(); for (x=0;x<combinedPointsArray.length;x++) { if (combinedPointsArray[x] >= biggest ) { values[biggest]++; biggest = parseInt(combinedPointsArray[x]); } } if ( values == biggest ) { document.write('A Dance-off is required <BR><BR> '); } document.write('The biggest score was : ' + (biggest)); </script> <BR><BR> <table border="1"> <tr><th>Names</th><th>Combined Points</th></tr> <script language="JavaScript"> <!-- // Use a for loop to step through the two arrays and print // the values in the table. for (var i=0; i<contestantNamesArray.length; i++) { document.write("<tr><td>" + contestantNamesArray[i] + "</td>"); document.write("<td>" + combinedPointsArray[i] + "</td></tr>"); } //--> </script> </HEAD> <BODY> </BODY> </HTML> Any help would be really appreciated. I have spent so long on this; consequently I am pulling my hair out. Regards hyp3r Hey, I'm writing a piece of code, using a given code as an example, but few things are confusing to me, if you could just explain me step by step what it does, I would really appreciated. Code: function getGeneticCodeString (type) { if ((type.toLowerCase() == "standard") || (type.toLowerCase() == "transl_table=1")) { return "/gc[acgturyswkmbdhvn]/=A," + // Here I know that the three letter combination HAS to begin with 'gc' // can be followed by a,c,t,g or u. // What do the other letter represent? "/[tu]g[ctuy]/=C," + // Begins with either 't' or 'u', middle symbol is 'g' // third symbol can be c, t, or u ('u' can be used only if it begins with 'u'. // what is the 'y' for in the second square brackets for?? "/ga[agr]/=E " ; // Must start with 'ga' followed by 'a' or 'g'. // What is 'r' for? } } Hope, I didn't confuse anybody. Thank you. I want to have another go at Javascript. I have several books on the subject but I find that my eyesight is a major problem. Therefore I want to try an on-line solution, preferably free. I have Googled, but there are so many that I am almost dizzy with the choices. Perhaps someone could recommend one. Not too fussy visually. My knowledge is VERY basic. Frank Hello! I am trying to find a script that allows you to open multiple browser tabs and then close each of those tabs, either one by one or all at once. Does anyone know how to do this please? Thanks so much for your help. Does anyone know how to make URL links that use Javascript still work when users have Javascript disabled on their browser? The only reason I'm using JS on a URL is because my link opens a PDF file, and I'm forcing it not to cache so users have the latest version. I tried the <script><noscript> tags, but I'm not sure if I'm using it correctly, as my URL completely disappears. Below is my HTML/Javascript code: <p class="download"> <script type="text/javascript">document.write("<span style=\"text-decoration: underline;\"><a href=\"javascript:void(0);\" onclick=\"window.open( 'http://www.webchild.com.au/mediakit/Direct_Media_Kit_Web.pdf?nocache='+ Math.floor( Math.random()*11 ) );\" >The Child Magazines Media Kit</a></span> (PDF 1 MB) ");</script> <noscript><span style="text-decoration: underline;"><a href="http://www.webchild.com.au/mediakit/Direct_Media_Kit_Web.pdf" >The Child Magazines Media Kit</a></span> (PDF 1 MB)</noscript> </p> Thanks for any help, Michael Hi Guys, I am new at JavaScript and start to do some tutorials.What I am trying to do here is prompting user to input a name and if the name was valid the page(document) will display with all objects like the button.But if user enter a wrong name then the button will be disabled! I create the following code but it did not work <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> <script language="JavaScript" type=""> function changeColor(){ document.bgColor = "Gray"; } </script> </head> <body> <script language="JavaScript" type="text/javascript"> var person = ""; person = prompt('What is Your Name:'); if (person == "Foo") { document.write("<h1 />Welcome " + person); document.bgColor = "Yellow"; } else { document.write("<h1 />Access Denied!!!!"); document.bgColor = "Red"; document.getElementById("gree").disabled = true; } </script> <div> <p/><input id="gree" type="button" value="Gray " onClick="changeColor();"> </div> </body> </html> as you can see I used the: document.getElementById("gree").disabled = true; but it did not work , could you please give an idea how I can solve this problem? Thanks |