javascript - Regular expression syntax difficulties -
I'm trying to learn regular expression testing in javascript but I'm hard to imagine. In my example given below, I am trying to test the user input for the first name to ensure that it does not have:
- Number
- More than 1 free space ('Sarah Jane' is OK, but not 'Sarah Jane') < / Ol>
This is my job till now, especially any explicit syntax errors, especially reg expression Stand out in this system? Thank you.
& lt; Script language = "javascript" type = "text / javascript" & gt; Validate the function name () {var input = document.forms ['myForm'] ['fName']. Values; Var re = / \ d \ s {2,} \ w /; If (input == "") {// Make sure some documents are filled. GetElementById ('fNameError'). Style.color = "red"; Document.getElementById ('fNameError'). InnerHTML = "** You must enter a name"; } And if (re-test) {document.getElementById ('fNameError'). Style.color = "red"; Document.getElementById ('fNameError'). InnerHTML = "** names should not contain more than one location between az or A-Z letters and names"; }} & Lt; / Script & gt;
Depending on your description, this regex will look better to you:
var re = / ^ [az] + ([az] +)? $ / I; Inverted as
and then
re.test :
else if (! Re.test (input)) { Document .getElementById ('fNameError'). Style.color = "red"; Document.getElementById ('fNameError'). InnerHTML = "** names should not contain more than one location between az or A-Z letters and names"; }
Comments
Post a Comment