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.

  1. a first name must entered field.

  2. a last name must entered field.

  3. an id number must entered here. value must begin “v” followed “0” (zero) followed 7 numbers.

  4. 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:&nbsp;&nbsp;  	<input id="rid" type="text" name="requiredid" placeholder="v0xxxxxxx" size="20" maxlength="20">   </p>  <p>  	4. enter username:&nbsp;&nbsp;  	<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

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -