number field validation - URGENT

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • simina

    #1

    number field validation - URGENT

    Hi...
    I have a form with 4 number fields: phone area, phone number, cell
    area, cell number.
    I did a function that checks the "number" issue for all 4 fields in
    the same time (because the code is onBlur: "return ph(this);"). Now
    the problem is that if the user hits Enter data still goes to the
    database (of course, because I should have the function called in the
    form's onSubmit......
    But how can I do this because I should now have 4 personalized
    functions for each field (maybe) or 1 function that comprise the code
    for all 4 fields??!!
    I am just lost...I started JavaScript 3 weeks ago and I'm in a
    profound agony...
    The code looks like this (and maybe even this one has a problem):
    function ph(obj){
    var checkVal="01234 56789";
    var objVal=obj.valu e;
    var ok=true;
    for(i=0;i<objVa l.length;i++)
    {
    var myChar=objVal.c harAt(i);
    for(j=0;j<check Val.length;j++)
    if (myChar==checkV al.charAt(j)) break;
    if(j==checkVal. length)
    {
    ok=false;
    break;
    }
    }
    if(!ok) //myChar is not in [0,9]
    {
    alert("This is not a valid value. Please enter a number!");
    obj.focus();
    return false;
    }
    return true;
    }


    Thanks...
  • Mick White

    #2
    Re: number field validation - URGENT

    simina wrote:
    [color=blue]
    > Hi...
    > I have a form with 4 number fields: phone area, phone number, cell
    > area, cell number.[/color]
    <snip>[color=blue]
    > The code looks like this (and maybe even this one has a problem):[/color]

    There is a core javacript function, "isNaN()"
    function ph(obj){
    return !isNaN(obj);
    // "obj" is a poor choice, it suggests an object.
    // "userEntry" may be a better choice
    }

    But I would suggest the use of regular expressions to verify your users'
    entries, for instance:
    A North American phone #
    function isNAPhone(userE ntry){
    return /^1?{9]$/.test(userEntry .replace(/[^\d]/g,""))
    }
    However this is a very crude test (an optional 1 followed by 9 numbers,
    after non-numerical characters are stripped from the user entry), a
    return of "true" does not guarantee a valid phone number (actually, I
    don't think it's possible)

    Mick[color=blue]
    > function ph(obj){
    > var checkVal="01234 56789";
    > var objVal=obj.valu e;
    > var ok=true;
    > for(i=0;i<objVa l.length;i++)
    > {
    > var myChar=objVal.c harAt(i);
    > for(j=0;j<check Val.length;j++)
    > if (myChar==checkV al.charAt(j)) break;
    > if(j==checkVal. length)
    > {
    > ok=false;
    > break;
    > }
    > }
    > if(!ok) //myChar is not in [0,9]
    > {
    > alert("This is not a valid value. Please enter a number!");
    > obj.focus();
    > return false;
    > }
    > return true;
    > }
    >
    >
    > Thanks...[/color]

    Comment

    • Randy Webb

      #3
      Re: number field validation - URGENT

      Mick White wrote:

      <--snip-->
      [color=blue]
      > A North American phone #
      > function isNAPhone(userE ntry){
      > return /^1?{9]$/.test(userEntry .replace(/[^\d]/g,""))
      > }
      > However this is a very crude test (an optional 1 followed by 9 numbers,
      > after non-numerical characters are stripped from the user entry), a
      > return of "true" does not guarantee a valid phone number (actually, I
      > don't think it's possible)[/color]

      You could test it to see if it falls in the acceptable pattern, but to
      tell if its a valid # or not, you would have to call it to see if it
      gets answered :)

      The Area Code got changed recently. At one time, it was always a 0 or 1
      for the 2nd digit, I know of a 334 (Alabama). It also never starts with
      a 0 or 1. So not sure what the exact rule is now :-\

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Michael Winter

        #4
        Re: number field validation - URGENT

        On 31 Aug 2004 21:01:47 -0700, simina <spetrusa@yahoo .com> wrote:
        [color=blue]
        > I have a form with 4 number fields: phone area, phone number, cell area,
        > cell number.
        > I did a function that checks the "number" issue for all 4 fields in the
        > same time (because the code is onBlur: "return ph(this);").[/color]

        Do not use the blur event to validate a field, especially when coupling it
        with the focus() method. At best, it traps the user in that control and at
        worst, it can lead to a deadlock where the user is bounced back and forth
        between two fields. Use the change event.

        Also, neither the blur event, nor the change event, can be cancelled.
        Returning the result of ph() in such cases it pointless.
        [color=blue]
        > Now the problem is that if the user hits Enter data still goes to the
        > database (of course, because I should have the function called in the
        > form's onSubmit......[/color]

        You should always be validating on the server. Javascript validation
        should just prevent a round trip whenever possible.
        [color=blue]
        > But how can I do this because I should now have 4 personalized functions
        > for each field (maybe) or 1 function that comprise the code for all 4
        > fields??!![/color]

        The answer depends on the nature of the format for each field. The fields
        should be tested with a regular expression, which is far more powerful
        (and quicker) than your code. If two or more fields share the same format,
        then they should share the same function. You'll need to specify the exact
        formats you expect if you need help in this matter.

        In the final form validation check, you would simply call all the
        functions, returning false if any failed.

        If you just want the fields to contain numbers only, your ph() function
        can be reduced to:

        function ph(elem) {
        if(!/^\d+$/.test(elem.valu e)) {
        alert('This is not a valid value. Please enter a number.');
        elem.focus();
        }
        }

        <input type="text" ... onchange="ph(th is);">

        [snip]

        Good luck,
        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • G Roydor

          #5
          Re: number field validation - URGENT



          simina a écrit:[color=blue]
          > Hi...
          > I have a form with 4 number fields: phone area, phone number, cell
          > area, cell number.
          > I did a function that checks the "number" issue for all 4 fields in
          > the same time (because the code is onBlur: "return ph(this);"). Now
          > the problem is that if the user hits Enter data still goes to the
          > database (of course, because I should have the function called in the
          > form's onSubmit......
          > But how can I do this because I should now have 4 personalized
          > functions for each field (maybe) or 1 function that comprise the code
          > for all 4 fields??!!
          > I am just lost...I started JavaScript 3 weeks ago and I'm in a
          > profound agony...
          > The code looks like this (and maybe even this one has a problem):
          > function ph(obj){
          > var checkVal="01234 56789";
          > var objVal=obj.valu e;
          > var ok=true;
          > for(i=0;i<objVa l.length;i++)
          > {
          > var myChar=objVal.c harAt(i);
          > for(j=0;j<check Val.length;j++)[/color]

          la condition j<checkVal.leng th

          [color=blue]
          > if (myChar==checkV al.charAt(j)) break;
          > if(j==checkVal. length)[/color]

          est en contradiction avec j==checkVal.len gth.
          En clair j==checkVal.len gth n'est jamais satisfait.

          GR
          [color=blue]
          > {
          > ok=false;
          > break;
          > }
          > }
          > if(!ok) //myChar is not in [0,9]
          > {
          > alert("This is not a valid value. Please enter a number!");
          > obj.focus();
          > return false;
          > }
          > return true;
          > }
          >
          >
          > Thanks...[/color]

          Comment

          • Dr John Stockton

            #6
            Re: number field validation - URGENT

            JRS: In article <nacZc.209905$b p1.77811@twiste r.nyroc.rr.com> , dated
            Wed, 1 Sep 2004 04:24:51, seen in news:comp.lang. javascript, Mick White
            <mwhite13@BOGUS rochester.rr.co m> posted :[color=blue]
            >simina wrote:
            >[color=green]
            >> Hi...
            >> I have a form with 4 number fields: phone area, phone number, cell
            >> area, cell number.[/color]
            ><snip>[color=green]
            >> The code looks like this (and maybe even this one has a problem):[/color]
            >
            >There is a core javacript function, "isNaN()"
            >function ph(obj){
            >return !isNaN(obj);
            >// "obj" is a poor choice, it suggests an object.
            >// "userEntry" may be a better choice
            >}
            >
            >But I would suggest the use of regular expressions to verify your users'
            >entries, for instance:
            >A North American phone #
            >function isNAPhone(userE ntry){
            >return /^1?{9]$/.test(userEntry .replace(/[^\d]/g,""))
            >}[/color]

            That tests for phone numbers in indigenous US/CA land-line format. But,
            AIUI, North America also includes Mexico - do they follow that format?
            And there are phones in NA which are not indigenous; maybe Jim's mobile.

            My newspaper tells me of those in the Netherlands who give up land-line
            phones, preferring a combination of broad-band Internet and mobile phone
            /* ? Does that give reliable access to emergency services, such as 999
            or 112 ? */. So an indigenous customer may not have a land-line number.
            [color=blue]
            >However this is a very crude test (an optional 1 followed by 9 numbers,
            >after non-numerical characters are stripped from the user entry), a
            >return of "true" does not guarantee a valid phone number (actually, I
            >don't think it's possible)[/color]

            On a practical definition of valid, it's not possible. A valid phone
            number can be converted to an unusable one, at least short-term, by
            burning down a house. But I imagine that numbers starting with the
            digits of a current special service, such as 1129999999, can only become
            valid as ordinary numbers in the rather distant future.

            ISTM that phone numbers can be checked only as being all-numeric (after
            stripping likely separators such as () - etc.) and at least a minimum
            length.

            --
            © John Stockton, Surrey, UK. ???@merlyn.demo n.co.uk Turnpike v4.00 MIME. ©
            Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.

            In MS OE, choose Tools, Options, Send; select Plain Text for News and E-mail.

            Comment

            • Mick White

              #7
              Re: number field validation - URGENT

              Dr John Stockton wrote:
              [color=blue]
              > JRS: In article <nacZc.209905$b p1.77811@twiste r.nyroc.rr.com> , dated
              > Wed, 1 Sep 2004 04:24:51, seen in news:comp.lang. javascript, Mick White
              > <mwhite13@BOGUS rochester.rr.co m> posted :[/color]
              <snip>[color=blue][color=green]
              >>A North American phone #
              >>function isNAPhone(userE ntry){
              >>return /^1?{9]$/.test(userEntry .replace(/[^\d]/g,""))
              >>}[/color]
              >[/color]
              [color=blue]
              > That tests for phone numbers in indigenous US/CA land-line format. But,
              > AIUI, North America also includes Mexico - do they follow that format?[/color]

              Mexico's format is quite different. Puerto Rico and some of the
              Carribean nations follow the US/Can format.
              Mick
              [color=blue]
              > And there are phones in NA which are not indigenous; maybe Jim's mobile.[/color]

              Comment

              Working...