Nov 21, 2009

Validate Numeric Only

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > JavaScript

Validate Numeric Only

TavoxPeru
Hi, this cool script created by: Manzi Olivier (http://www.imanzi.com/) verifies in real time that a input string is numeric, otherwise it deletes the non-numeric character. To configure it follow this steps:
  1. Paste this code into an external JavaScript file named: validNum.js
    CODE
    // calculate the ASCII code of the given character
    function CalcKeyCode(aChar) {
      var character = aChar.substring(0,1);
      var code = aChar.charCodeAt(0);
      return code;
    }

    function checkNumber(val) {
      var strPass = val.value;
      var strLength = strPass.length;
      var lchar = val.value.charAt((strLength) - 1);
      var cCode = CalcKeyCode(lchar);

      /* Check if the keyed in character is a number
         do you want alphabetic UPPERCASE only ?
         or lower case only just check their respective
         codes and replace the 48 and 57 */

      if (cCode < 48 || cCode > 57 ) {
        var myNumber = val.value.substring(0, (strLength) - 1);
        val.value = myNumber;
      }
      return false;
    }
  2. Paste this code into the HEAD section of your HTML document. You may need to change the path of the file.
    CODE
    <script type="text/javascript" src="validNum.js"></script>
  3. Paste this code into the BODY section of your HTML document
    CODE
    <form name="myForm" method="post" action="#">
      Enter an integer here: <input name="txtNumber" type="text"
        id="txtNumber" onKeyUp="java script:checkNumber(myForm.txtNumber);">
    </form>
Of course, you need to add a submit or a button field and code its respective onclick or onsubmit event to really perform the submission of the data entered, but that is very easy to do, no?.

Best regards,

 

 

 


Comment/Reply (w/o sign-up)

Arbitrary
Ahh...now this would be really useful for when collecting phone numbers and wishing to remove all the non numeric characters from it. I just read at AListApart that it would be friendlier to users if forms simply allowed users to fill in their phone number in whatever format they pleased. Thus, this script might prove useful. On a second note, it is Javascript, and I was thinking of sticking more to PHP, as Javascript could still be a usability problem for some of the security-freaked visitors. At least I know the server can always run my PHP scripts. smile.gif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Validate Numeric


    Looking for validate, numeric

See Also,

*SIMILAR VIDEOS*
Searching Video's for validate, numeric
advertisement



Validate Numeric Only

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com