javascript - Verifying input to form -
i’m trying add additional verification requirement form works correctly.
right javascript checks make sure form has following values before submitted.
a first name must entered field.
a last name must entered field.
an id number must entered here. value must begin “v” followed “0” (zero) followed 7 numbers.
a user name must entered field.
my concern #3. i’d add additional requirement produces dialog message if value of field not begin “v” followed either “0” or “9” , followed 7 numbers.
for example, these values work:
v01234567 v91234567
these not work:
v0wewewew v11234567
i have tried changing:
if(rid.charat(0) != 'v' || rid.charat(1) != '0'){
to this
if(rid.charat(0) != 'v' || rid.charat(1) != '0' || rid.charat(1) != '9'){
but doesn’t work.
i appreciate help.
thanks, mike
<script type="text/javascript"> function checkrequired(which) { var fn = document.getelementbyid("fn").value; var ln = document.getelementbyid("ln").value; if(fn == ''){ alert("please enter first name"); return false; } if(ln == ''){ alert("please enter last name"); return false; } var rid = document.getelementbyid("rid").value; var nb = rid.substring(2); if(rid.charat(0) != 'v' || rid.charat(1) != '0'){ alert("enter id correctly: v0 followed 7 numbers."); return false; }else if(nb.length != 7){ alert("enter id correctly: v0 followed 7 numbers."); return false; }else if(isnan(nb)){ alert("enter id correctly: v0 followed 7 numbers."); return false; } var un = document.getelementbyid("un").value; if(un == "enter username" || un == ''){ alert("please make sure entered valid username"); return false } } </script> </head> <body> <div id="wrapper"> <div id="main_content"> <form action="page2.html" method="post" name=myform onsubmit="return checkrequired(this)"> <p> enter first name, last name, , identification number (id) spaces below, , click continue button. </p> <p> 1. first name: <input id="fn" type="text" name="requiredfirstname" value="" size="20" maxlength="20"> </p> <p> 2. last name: <input id="ln" type="text" name="requiredlastname" value="" size="20" maxlength="20"> </p> <p> 3. enter identification number: <input id="rid" type="text" name="requiredid" placeholder="v0xxxxxxx" size="20" maxlength="20"> </p> <p> 4. enter username: <input id="un" type="text" name="requiredusername" placeholder="enter username" size="15" maxlength="50"><strong>@mail.mydomain.com</strong> </p> <p> <input type="submit" name="submit" value="continue"> </p> </form> </div> </div>
try:
if (!/^v[09]\d{7}$/.test(rid)){ alert("enter id correctly: v0 followed 7 numbers."); return false; }
here's tests:
function isvalid(rid) { return /^v[09]\d{7}$/.test(rid) ? rid + " valid" : rid + " invalid"; } var array = []; array.push(isvalid("v01234567")); array.push(isvalid("v91234567")); array.push(isvalid("v0wewewew")); array.push(isvalid("v11234567")); alert(array.join("\n"));
Comments
Post a Comment