www.webdeveloper.com
Results 1 to 5 of 5

Thread: Validate input field - must be only digits

Hybrid View

  1. #1
    Join Date
    Sep 2009
    Posts
    51

    Validate input field - must be only digits

    Hi,

    How can I validate a text field to make sure it contains only digits?

    Thanks,

  2. #2
    Join Date
    Feb 2006
    Posts
    2,926
    field={value:'12e'}
    isdigits=parseInt(field.value)+''===field.value;
    alert(isdigits)

  3. #3
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    Code:
    <script language="javascript" type="text/javascript">
    	function isNumeric(sText, decimalAllowed) {
    		if (sText.length == 0) return false;
    		var validChars = "";
    		if (decimalAllowed) {
    			validChars = "0123456789.";
    		} else {
    			validChars = "0123456789";
    		}
    		var isNumber = true;
    		var charA;
    		var decimalCount = 0;
    		for (i = 0; i < sText.length && isNumber == true && decimalCount < 2; i++) {
    			charA = sText.charAt(i); 
    			if (charA == ".") { 
    				decimalCount += 1;
    			}
    			if (validChars.indexOf(charA) == -1) {
    			isNumber = false;
    			}
    		}
    		return isNumber;
    	}
    </script>

  4. #4
    Join Date
    Sep 2008
    Location
    Cali
    Posts
    14
    HTML Code:
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="js/jquery.validate.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    	$("#myform").validate({
    	  rules: {
    		mydigitfield: {
    		  required: true,
    		  digits: true
    		}
    	  }
    	  , messages: { 
    		   mydigitfield: {
    			   digits: 'Digits only there bud'
    		   }    
    		}
    	});
    });
    </script>
    <form name="myform" id="myform" method="post">
    <input type="text" name="mydigitfield" id="mydigitfield" />
    <input type="submit" />
    </form>
    mostly from http://docs.jquery.com/Plugins/Valid...Methods/digits
    Last edited by philmee95; 02-05-2010 at 04:36 PM.

  5. #5
    Join Date
    Sep 2005
    Posts
    73
    Hi
    The most EASY way is: NaN or isNaN()
    which is Not A Number
    http://www.w3schools.com/jsref/jsref_nan.asp

    Good Luck!
    E.c
    http://funny-video.tv

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles