validate text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seethal
    New Member
    • Aug 2012
    • 1

    validate text box

    how to validate a text box compare with the values stored in an array. the text box is for username.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    what do you have done so far? please post your code. Basicly you need to decide when the validation should be executed - so find an event where you register the validation handler and where you then implement the validation logic.

    Comment

    • ariful alam
      New Member
      • Jan 2011
      • 185

      #3
      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>
      hope it's help you. :)

      Comment

      Working...