Validate numeric fields using JavaScript.

Source: Internet
Author: User
Validate numeric fields using JavaScript.
By nannette Thacker-1/18/2001

Use the onblur () method to validate numeric fields with this checknumeric () JavaScript function. optionally pass in a period, comma, or hypen to indicate allowed values. set the maximum and minimum value allowed.

If your allowed values always include period, comma, or hyphen, or never include those, you may easily edit the function to remove the parameter values from the onblur () and function.

You may also use a variation of this routine to check for any combination of valid values. You don't have to use "0123456789"-you may instead use something else as your check string.

Here is the javascript:

<Script language = "JavaScript"> <! -- Original: nannette Thacker --> <! -- Http://www.shiningstar.net --> <! -- Beginfunction checknumeric (objname, minval, maxval, comma, period, hyphen) {var numberfield = objname; If (chknumeric (objname, minval, maxval, comma, period, hyphen) = false) {numberfield. select (); numberfield. focus (); Return false;} else {return true;} function chknumeric (objname, minval, maxval, comma, period, hyphen) {// only allow 0-9 be entered, plus any values passed // (can be in any order, and don't have to B E comma, period, or hyphen) // if all numbers allow commas, periods, hyphens or whatever, // just hard code it here and take out the passed parametersvar checkok = "0123456789" + comma + period + hyphen; var checkstr = objname; var allvalid = true; var decpoints = 0; vaR allnum = ""; for (I = 0; I <checkstr. value. length; I ++) {CH = checkstr. value. charat (I); For (j = 0; j <checkok. length; j ++) if (CH = CH Eckok. charat (j) break; If (j = checkok. Length) {allvalid = false; break;} If (Ch! = ",") Allnum + = CH;} If (! Allvalid) {alertsay = "Please enter only these values \" "alertsay = alertsay + checkok +" \ "in the \" "+ checkstr. name + "\" field. "alert (alertsay); Return (false);} // set the minimum and maximumvar chkval = allnum; var prsval = parseint (allnum); If (chkval! = ""&&! (Prsval> = minval & prsval <= maxval )) {alertsay = "Please enter a value greater than or" alertsay = alertsay + "equal to \" "+ minval +" \ "and less than or" alertsay = alertsay + "equal \ "" + maxval + "\" in the \ "" + checkstr. name + "\" field. "alert (alertsay); Return (false) ;}// end --> </SCRIPT>
 

Here is the HTML:

<Form method = "Post" id = myform name = myformaction = "/articles/JavaScript/checknumeric. asp? Id = <% = siteid %> "> allow comma, period, negative <input type = text onblur =" checknumeric (this ,',','. ','-'); "name = 'allowcomm' size = 10 maxlength = 10> <br> no comma, period, negative <input type = text onblur = "checknumeric (this, 5, 60 ,'','',''); "Name = 'nocomm' size = 10 maxlength = 10> <br> <input type =" Submit "name =" save "value =" save "> </form>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.