Form validation with Javascript - Field value must be between 2 specific numbers -
I have a form on a page, there is an area called Birthday on the farm. I need to know the following in javascript:
When the value field entered between 1900-2012 is entered, the form is accepted. If the price is not between 1900-2012, then the birthday text box background color will be yellow.
Does anyone have any idea how to achieve this?
My HTML is following:
& lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "div1" & gt; & Lt; Form name = "myForm" action = "demo_form.php" onsubmit = "return validateForm ()" method = "post" & gt; Username: & lt; Input type = "text" id = "user name" name = "user name" & gt; & Lt; Br> Birth year: & lt; Input type = "article" id = "birth year" name = "year of birth" & gt; & Lt; Br> & Lt; Br> & Lt; Input type = "submit" value = "submit" & gt; & Lt; / Form & gt; & Lt; / Div & gt; & Lt; Div id = "div2" & gt; & Lt; Img src = "cat.jpg" id = "im1" name = "picture 1" /> & Lt; Img src = "dog.jpg" id = "im2" name = "image2" /> & Lt; Img src = "fish.jpg" id = "im3" name = "image3" class = 'double' / & gt; & Lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt; My JS is following:
document.getElementById ("user name"). Focus (); Validate function () {var x = document.forms ["myForm"] ["user name"]. Values; If (x == "") {Warning ("username is required!"); . Document.getElementById ("username") focus (); } Var dob = document.forms ["myForm"] ["birth year"]; Var y = dob.value; If (INBA (Dob)) {Warning ('must be a number'); Dob.select (); return false; }}
validate the function () {var x = document.forms [. "MyForm"] ["user name"] value; If (x == "") {Warning ("username is required!"); . Document.getElementById ("username") focus (); } Var dob = document.forms ["myForm"] ["birth year"]; Var y = dob.value; If (! IsNa (y)) {if (y> = 1900 & amp; y & lt; = 2012) {// right} else {// Wrong document .getElementById ("Birth year"). Style.background = "yellow"; Document.getElementById ("birthYear") focus () .; return false; }} And {warnings ('must be a number'); }}
Comments
Post a Comment