validation for textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepikashalini
    New Member
    • Mar 2008
    • 18

    validation for textbox

    Hi,

    how to validate the textbox accepts 10 Numbers only.
    give sample code....
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    Which ten numbers do you allow?

    10 digits?
    Code:
    inputelement.onchange=function(e){
    	e= window.event || e;
    	e= e.target || e.src;
    	var M= /^(\d{10})$/.exec(e.value);
    	return e.value= M? M[1]: '';
    }

    Comment

    • malav123
      New Member
      • Feb 2008
      • 217

      #3
      Hi,
      just set the maxlength of textbox = 10 and to check that user has entered the 10 digits write following code in javascript...
      [CODE=javascript]
      function check()
      {
      if(textbox1.val ue.length!=10)
      {
      alert("Please Enter 10 digits for textbox1");
      textbox1.focus( );
      return false;
      }
      }
      [/CODE]
      Last edited by malav123; Apr 8 '08, 12:26 PM. Reason: to set format of javascript coding

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        Originally posted by malav123
        Hi,
        just set the maxlength of textbox = 10 and to check that user has entered the 10 digits write following code in javascript...
        [CODE=javascript]
        function check()
        {
        if(textbox1.val ue.length!=10)
        {
        alert("Please Enter 10 digits for textbox1");
        textbox1.focus( );
        return false;
        }
        }
        [/CODE]
        that code just checks for 10 characters without checking the type of it ... so it would be possible to enter 10 alpha-chars or numbers or a mixture ... so mrhoo's (see post #2) solution is to prefer for the original requirement ...

        kind regards

        Comment

        • malav123
          New Member
          • Feb 2008
          • 217

          #5
          Originally posted by gits
          that code just checks for 10 characters without checking the type of it ... so it would be possible to enter 10 alpha-chars or numbers or a mixture ... so mrhoo's (see post #2) solution is to prefer for the original requirement ...

          kind regards
          Yes you are right i have only specified for length 10, for numeric value checking "isNaN" is very useful function of javascript... right ?

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            yup ... you are right ... using isNaN() or a regExp like mrhoo did are the common ways to check such things ...

            kind regards

            Comment

            Working...