Results 1 to 3 of 3

Thread: Function for validating a number

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Function for validating a number

    I have made a function for validating a text field. It is required that it is numeric. I have tried the code below, but the alert is activated when a "-" is entered. Can I use a IsNumeric method like in VB?
    Code:
    ...
    var err3 = 0;
    var allowedchars = /\D/;
    if ((allowedchars.test(document.cond.luftf.value)) || (allowedchars.test(parent.frames[1].navform.flow.value))) {
    alert("ERROR");
    err3 = 1;
    ...
    }

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Code:
    <SCRIPT LANGUAGE="JAVASCRIPT"><!--
    Function check(contents) {
        If (((contents / contents) != 1) && (contents != 0)) {alert('Please enter only a number into this text box')}
    }
    //--></SCRIPT>
    Mark
    -------------------

  3. #3
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    You can modify you matching string. If you want to allow numbers and -'s. Using parseInt() will give unpredictable results if you want to allow -'s. So will trying to divide the string into itself.

    Let's say you wanted to match a US telephone number:

    Code:
    var allowed = /\(?\d{0,3}\)?-?\d{3}-\d{4}/;
    You might have to escape the -'s, I'm not sure.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

Posting Permissions

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



Click Here to Expand Forum to Full Width