JavaScript - Regular Expressions - Detect Letters In String
Hey I'm trying to make a script which will check if the string which the user sent from a form contains ONLY letters. The problem is if the user entered something like this "25 years old" it allow that to be submitted. It only blocks submissions of the form if the user submits NO letters like this "12345". I want it to block submissions if at least one character isn't a letter. Here's my code:
Code: var message = document.myform.formtest.value; var allowed = /[A-z+]/; if(!allowed.test(message)) { alert("The field only allows letters (a-z)."); return false; } Similar TutorialsHi wise javascript gurus, I am working on a small form that on the click of a button, it calls a function to validate a string that consists of 3 portions delimited with a dash("-") in the following format XXX-NNNN-XXX where X are letters only where N are numbers only I have to use regular expressions to validate the first portion and verify that its exactly 3 characters long that are letters only if the first portion or "plant code" is larger then 3 characters long an alert gets display also if the first portion contains alphanumeric or numeric characters an alert gets displayed, the first portion has to be in letters only. down below is my code that is not functional at the time because I know for sure the way I check for regular expressions is a bet washy but thats the only thing I was able to come up with, any help will be appreciated. thanks for making time to read my thread Code: <html> <head> <script type="text/javascript"> function validateit() { var number = document.myform.product_number.value; var charnum = number.length; if (number =="") alert("you must enter a product number, field cannot be empty"); else if (charnum < 12 || charnum > 12) alert("the product numbre has to be exactly 12 characters long"); else validate_number(number); } function validate_number(num) { var objRegEx = /^[a-z]+$/; split=num.split("-",3); if (split[0].length!=3) alert("the Plant Code portion of the Product Number has to be only three charactes long") if!(objRegEx.test(split[0]) alert("the Plant Code portion of the Product Number has to be letters only") } </script> <title>waldo's</title> <style type="text/css"> .heading{font-weigth:bold;text-align:center; font-family:"comic sans MS"} </style> </head> <body><form name="myform"> <table align="center" border="1" width="55%"> <caption style="font-family:'comic sans MS';font-size:26pt"> Waldos's WareHouse</caption> <tr class="heading"><td>Product Number</td><td>Plant Code</td><td>Production Run</td><td>Shift Number</td></tr> <tr><td><input type="text" name="product_number"></td><td><input type="text" name="plant_code" readonly></td><td><input type="text" name="production_run" readonly></td><td><input type="text" name="shift" readonly></td></tr> <tr><td><input type="button" value="Check Validity" onclick="validateit()"/></td><td align="right">Validation Date:</td><td colspan="2"><input type"text" size="47" readonly></td></tr> </table></form> </body> </html> Hi, can anyone tell me how to detect any word (a-z only, surrounded by whitespace) less than 5 letters long? Edit - this is what I have currently: Code: var RE=/\s([a-z]{1,4})\s/gi; var string="some words"; //should be turned to "words" string=string.replace(RE, ""); This is taking 4 letter chunks from the end of words. Cheers, Gus Edit - fixed (replaced \s with \b) Dear Experts I have folloiwng codes Code: <html> <head> <body> <center> <script language="JavaScript"> function checkpostal(){ var re=/^\D{A-Z}$/ if (myform.myinput.value.search(re)==-1) { alert("Good") }else{ alert("Bad") } } </script> <form name="myform"> <input type="text" name="myinput" size=15> <input type="button" onClick="checkpostal()" value="check"> </form> </center> </body> </html> With Onclick, I want to test input through regular expression If entered values is between a-z or A-Z then it must display alert("Good") else alert("Bad") Please help Hello. So I have been doing some regex in php. In Javascript i need to parse a url and replace ALL the & with \/%26 (or what ever that one is) and replace ALL the + with the % thingy. But my problem is I am doing url.replace(/&/,'\/%26') but what is happening is it is replace only the FIRST &. How can I do it to replace ALL &? I was thinking of doing some type of loop but for some reason nothing is working...THANKS IN ADVANCE! Can someone please explain the following code to me!? I don't get regular expressions and how they work :S Code: function trim(string){ return string.replace(/^\s*/, "").replace(/\s*$/, ""); } Thanks! How can I get price from a string using regular expression ? Or by any other means . Let's say. var str="Our shop receives only $. This shoe costs $200. We don't accept anything else then $"; How could I retrieve '$200' from this above string. By any means. Thank you... Hi everyone, I am trying to validate a date by using regular expressions. I have parts working, such as only accepts numbers, but I cannot get the range correct. On the "mm" field, I am getting errors. Code: if(form.mm.value=="") { alert("Please insert your birth month"); return false;} var re=/^(0[1-9]|1[0-2]){2}$/; if(!re.test(form.mm.value)) { alert("Please fix birth month!"); return false; } { if(form.dd.value=="") { alert("Please insert your birth day"); return false;} var re=/^\d{2}$/; if(!re.test(form.dd.value)) { alert("Please fix birth day!"); return false; } { if(form.yyyy.value=="") { alert("Please insert your birth year"); return false;} var re=/^\d{4}$/; if(!re.test(form.yyyy.value)) { alert("Please fix birth year!"); return false; Any suggestions will be greatly appreciated. Hey everyone I am trying to match an email string that ends exactly with ".com". Here's what I have Code: var email = window.prompt("Enter your email", ""); var email_match = /[a-zA-Z1-9-_.]{3,}\@[a-zA-Z]{3,}\.(com)/ if (email_match.test(email)) alert("Email is Valid"); else alert("Email Invalid!"); the (com) also matches commm for some odd reason. What must I change in the code so that only emails ending with .com is valid? Thanks! Hi I've got two examples of using regular expressions one works and the other doesn't but they using exactly the same pattern and the same text only difference is - The example that works uses a form to get the patern and text, the other has the pattern hard coded. Any help will be greatly appreciated Works - Code: <html> <head> <title></title> <style type="text/css"> </style> <script type="text/javascript"> <!-- function frmTestClick() { var re = new RegExp(document.frmTest.regex.value,"gi"); if (document.frmTest.subject.value.match(re)) { alert("Successful match"); } else { alert("No match"); } } function demoShowMatchClick() { var re = new RegExp(document.frmTest.regex.value,"gi"); var m = re.exec(document.frmTest.subject.value); if (m == null) { alert("No match"); } else { var s = "Match at position " + m.index + ":\n"; for (i = 0; i < m.length; i++) { s = s + m[i] + "\n"; } alert(s); } } function demoReplaceClick() { var re = new RegExp(document.frmTest.regex.value, "gi"); document.frmTest.result.value = document.frmTest.subject.value.replace(re, document.frmTest.replacement.value); } //--> </script> </head> <body> <div id="content"> <form name="frmTest" id="frmTest" action="" method="post"> <p>Regexp: <input type="text" name="regex" value="</?(b|strong|font|span)\b(.|\n)*?>" size="50"></p> <p>Subject string: <input type="text" name="subject" value="<font>the quick brown fox jumped over <span>the</span> lazy dog</font>" size="50"></p> <p><input type="button" value="Test Match" onclick="frmTestClick()"> <input type="button" value="Show Match" onclick="demoShowMatchClick()"></p> <p>Replacement text: <input type="text" name="replacement" value="replaced" size="50"></p> <p>Result: <input type="text" name="result" value="click the button to see the result" size="50"></p> <p><input type="button" value="Replace" onclick="demoReplaceClick()"></p> </form> </div> </body> </html> Doesn't work??? Code: <html> <head> <title></title> <style type="text/css"> </style> <script type="text/javascript"> <!-- function stripHTML(sVal,bIgnoreBold){ var sPat="</?(b|strong|font|span)\b(.|\n)*?>",re = new RegExp(sPat,"gi"); if (sVal.match(re)) { alert("Successful match"); } else { alert("No match"); } return sVal.replace(re,""); } //--> </script> </head> <body> <div id="content"> <form name="frmTest" id="frmTest" action="" method="post"> <label>Body: </label><textarea name="txtBody" id="txtBody" rows="10" cols="60"><font>the quick brown fox jumped over <span>the</span> lazy dog</font></textarea><br><br> <input type="button" name="btnDoStrip" id="btnDoStrip" value="Do Strip" onclick="this.form.txtBody.value=stripHTML(this.form.txtBody.value,true);"> <input type="reset" name="btnReset" id="btnReset" value="Reset"> </form> </div> </body> </html> Cheers Al Hi All I need help with javascript regular expressions. I have a text box in which user can enter only 4 types of values. 1) Varchar(n) where 0 < n <=80. Ex: Varchar(20) (Varchar(n) . 0< n<= 80 means he can enter values onlly like varchar(1) or varchar(2) ..... varchar(80) ) 2) Char(n) where 0 < n <=80. Ex: Char(20) 3) numeric(x,y) where x>y and 1<= x <=35 and 0<= y <=15. Ex: numeric(20,6) If the text box value doesnot match this pattern, we should throw him alert message. Thanks in Advance Hi, i'm new to javascript, im managing to find out how to do most things, but i need to validate two paths. I want to know how to check whether path1 is a subpath of path2. e.g. 1: /files/stuff/mypics/anotherfolder/etc 2:/files/stuff/mypics i want to be able to return a boolean given whether i can move folder 1 in to folder 2 in this case yes. but if you swap them its a no. please help. oxide Hello everyone, I'm new to using regular expressions with javascript. I'm trying to search a string for instances of a substring with a particular prefix, followed by some numerical digits. So for instance my larger_string is: "This is a string ABCD12 EFGH124 ABCD76 EFGH90" And initially I want to find all substrings starting 'ABCD', with two following numerical digits. I can do it like this: var answer = larger_string.match(/ABCD\d\d/); But I'd really like to pass the 'ABCD' part in as a string itself, myString, as I'd then like to set myString to 'EFGH' and others, to repeat the search for those. I'm struggling with the syntax for creating a regular expression froma string. So I can do this: var reg = new RegExp("ABCD", "g"); but, this doesn't work: var reg = new RegExp("ABCD\d\d", "g"); and similarly I can do this: var myString = "ABCD"; var reg = new RegExp(myString, "g"); but, this doesn't work: var myString = "ABCD\d\d"; var reg = new RegExp(myString, "g"); What is it that I'm doing wrong here? Thank you Hi I have finally figured how to search a text area for URL's... as below, then reformat so they are clickable links. It works!! But it looks so so messy, is there a better way to do this?? Code: var textNodeLinks = document.getElementById("comments"); textNodeLinks.innerHTML = textNodeLinks.innerHTML.replace(/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/g, "$1<a href=http://$2$3\ rel="nofollow" target=_blank>$2$3</a>"); textNodeLinks.innerHTML = textNodeLinks.innerHTML.replace(/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/g, "$1$2<a href=\"$3\" rel="nofollow" target=_blank>$3</a>"); Cheers I have a string of code like such: Code: <font color=black>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</font><br><font color=black>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</font><br><font color=black>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</font><br><font color=black>000000000000000000000000000000000000000000000</font><font color=white>0000</font><font color=black>000000000000000000000000000000000000000000000</font><br><font color=black>000000000000000000000000000000000000</font> except much longer, like... 4000 lines longer. What I want to do is go through this code and replace all the zeros between <font color=black> and </font> with 1's and leave anything between <font color=white> and </font> as 0's. I just really need the regular expression not the code. I started reading a book on regular expressions and it's like... 300 pages, so it would be nice to solve this problem now, rather than have to wait to finish the book to solve this. Hopefully when I'm done reading it will make sense, but can someone help me in the meantime? Hello, How I can detect URLs in a larger body of text using a regular expression in javascript. For ex.: I type this text in my form and I saved in database: My home page is http:// www. mypage .com.it And in my site I want to appear like he My home page is http://www.mypage.com.it Hi, I'm very new to javascript, and I'm working on a simple script that will allow me to take a string of numbers, and convert it to a word. For example: I type 00010203 into a box, and it would return abcd. I've got the above to work, however..I'd like to add a couple special characters so that when the user types in a string that contains "32" it will return a ' ' (space). For example: 000102033200010203 would return 'abcd abcd' Here's my code, as of now: Code: <script type="text/javascript"> function show_prompt() { var s = prompt("Please enter a string of numbers"); for (i = 0; i <=s.length; i = i+2) { var t = s.slice(i,i+2) var n = parseInt(t,10) + 97 document.write (String.fromCharCode(n)) } } </script> Hi all, Forgive my ignorance, I'm quite new to Javascript... I'm looking to do two things: the first is to move elements of an array to the next index using a function. I have managed to do this like so: Code: var letters = ['h','i','j','k']; function moveElements(anArray){ var newArray = anArray; newArray.unshift(newArray[newArray.length-1]); newArray.pop(); } moveElements(letters); (this may or not be great, but it seemed to work) ... but I want the original array to reset to its original state if the function is called again (thus producing the same answer as opposed to shifting the elements again). Secondly, and this is the bit I'm really struggling with: I want to compare each letter of a string to elements within an array and display the indices. For instance: Code: var myArray = ['a', 'b', 'c', 'd', 'e', 'f']; var text = 'dead'; Hence I want the result '3,4,0,3'. I know the answer will use for and if statements but I can't wrap my head around it at all. Help would be appreciated, thank you. I want to - after the page has loaded - detect a text string in the code.. something similar to Code: function afterPageLoadsDetect() { Quote: Simply put -- I want javascript to detect a text string in the source code and return it to me -- AFTER the page is fully loaded. THANKS. I have a .c file with many function calls like, say, "ABC ( x, y, z);" My concern is to extract just the function name ABC. My approach is to read the file using streamreader.ReadToEnd(), and find the function name using regular expression \b\w+(\([a-z]*[0-9]*\)\b\w). the function call may be of the form... ABC ( xab, ybc, zbc, hgs );" i.e. the syntax is spread thru multiple lines. in the end i just want the function name ABC. please help me with the regular expression or if there is any other approach. Thank U |