how to validate a text box compare with the values stored in an array. the text box is for username.
validate text box
Collapse
X
-
Try the below code.
Code:<html> <head> <title>check array contains</title> <script language="JavaScript"> var ids=new Array("name1","name2","name3"); function validate(userid) { if(userid=="") { alert('user id is not given.'); } else if(contains(ids,userid)) { document.getElementById('msg').innerHTML="<b>"+userid+"</b> found."; }else{ document.getElementById('msg').innerHTML="<b>"+userid+"</b> not found."; } } // contains function function contains(arr, findValue) { var i = arr.length; while (i--) { if (arr[i] === findValue) return true; } return false; } </script> </head> <body> <div id='msg'></div> <label>User ID: <input type='text' size='30' id='userid' name='userid' placeholder='please enter your id'/></label> <input type='submit' name='login' value='check' onclick="javascript:validate(document.getElementById('userid').value);"/> </body> </HTML>
Comment
Comment