Javascript to Validate the Phone Number Field Using Javascript :-)

Discussion in 'Javascript' started by Abhishek, Aug 5, 2008.

  1. Abhishek

    Abhishek Guest

    Hi this is my another validator in javascript to validate the Phone
    Number :)


    <script language='javascript'>
    function funcCheckPhoneNumber(ctrtxtMobile,e){
    if(window.event){
    var strkeyIE = e.keyCode
    if(((strkeyIE >= 48) && (strkeyIE <= 57 )) || (strkeyIE >= 40) &&
    (strkeyIE <= 41 ) || (strkeyIE == 32) || (strkeyIE == 46)||(strkeyIE
    == 45) ){}
    else{
    return false;}}
    else{
    var strkeyCode = e.keyCode
    var strCharCode = e.charCode
    if(((strCharCode >= 48) && (strCharCode <= 57 )) || ((strCharCode
    >= 40) && (strCharCode <= 41 )) || (strCharCode == 45) ||

    (strCharCode==32)|| (strkeyCode==37 )|| (strkeyCode==38) ||
    (strkeyCode == 46)||(strCharCode==46)||(strkeyCode==8 ) || (strkeyCode
    ==9 ) ||(strkeyCode==39) || (strkeyCode ==35) || (strkeyCode ==36) ||
    (strkeyCode==9)){}
    else{
    return false;}}
    return true;}

    function valFuncReg_PhoneNumber(text,reg){
    if(text == null || text == '')return true;
    if(reg == null || reg =='')return true;
    var regex = new RegExp(reg);var value=text;
    var res= (regex.exec(text));
    if(res==null){
    reg =/^\d*$/;
    regex = new RegExp(reg);
    res= (regex.exec(text));
    }
    return (res != null && value == res[0]);}
    </script>

    The above script can be called on "Onkeypress" event of the TextBox as
    follows :

    "Onkeypress", "javascript:return funcCheckPhoneNumber(this,event);"


    Please feel free to give Comments and Suggessions and Bugs also :)
     
    Abhishek, Aug 5, 2008
    #1
    1. Advertising

  2. Re: Javascript to Validate the Phone Number Field Using Javascript

    Abhishek wrote:
    > Hi this is my another validator in javascript to validate the Phone
    > Number :)
    > [...]
    > Please feel free to give Comments and Suggessions and Bugs also :)


    Please stop posting your clueless, invalid junk code (here). Thanks in advance.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Aug 5, 2008
    #2
    1. Advertising

  3. Abhishek

    RobG Guest

    Re: Javascript to Validate the Phone Number Field Using Javascript

    On Aug 5, 4:33 pm, Abhishek <> wrote:
    > Hi this is my another validator in javascript to validate the Phone
    > Number :)


    It is much more efficient and a lot less code to use a regular
    expression, something like:

    function isPhoneChars(n) {
    return /^[0-9 ()]*$/.test(n);
    }

    Forms are much more usable if you do not prevent input if errors are
    detected, warn users of errors and let them fix it themselves, e.g.


    <script type='text/javascript'>

    function isPhoneChars(n) {
    return /^[0-9 ()]*$/.test(n);
    }

    function validate(el) {
    if (el.id == 'phone') {
    var errEl = document.getElementById('phone_ErroMessage');
    if (!isPhoneChars(el.value)) {
    errEl.style.visibility = 'visible';
    } else {
    errEl.style.visibility = 'hidden';
    }
    }
    }

    </script>


    <input type="text" id="phone" onkeyup="validate(this);">
    <span id="phone_ErroMessage"
    style="color: red; font-weight: bold; visibility: hidden;"
    >Phone number must be digits, spaces and brackets only</span>



    A quick example only, not intended for production use. Validate at
    the server always.


    --
    Rob
     
    RobG, Aug 5, 2008
    #3
  4. Re: Javascript to Validate the Phone Number Field Using Javascript :)

    In comp.lang.javascript message <619396d5-74a5-4b69-8d81-3e22cf669744@a8
    g2000prf.googlegroups.com>, Tue, 5 Aug 2008 00:05:36, RobG
    <> posted:
    >
    >function isPhoneChars(n) {
    > return /^[0-9 ()]*$/.test(n);
    >}


    That accepts unreasonably short numbers, including "". There is
    probably a known minimum length for phone numbers - the Falklands seem
    to use 5 digits internally, for example, and Ascension maybe 4. There's
    also, IIRC, a maximum allowable length, maybe larger than expected. The
    OP should consult ITU recommendations before proceeding.

    That allows characters ( ) which cannot be dialled. For consistency, it
    should also allow the characters + and - which occur in standard and/or
    common forms of printed number.

    Since, as well as 0 1 2 3 4 5 6 7 8 9, there are keys for * and #, could
    it be that those also ought to be allowed?

    <URL:http://www.merlyn.demon.co.uk/js-valid.htm#VTN> refers. Also
    <http://en.wikipedia.org/wiki/Telephone_numbers>.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Aug 5, 2008
    #4
  5. Abhishek

    Abhishek Guest

    Re: Javascript to Validate the Phone Number Field Using Javascript

    On Aug 5, 11:51 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > Abhishek wrote:
    > > Hi this is my another validator in javascript to validate the Phone
    > > Number :)
    > > [...]
    > > Please feel free to give Comments and Suggessions and Bugs also :)

    >
    > Please stop posting your clueless, invalid junk code (here). Thanks in advance.
    >
    > PointedEars
    > --
    > realism: HTML 4.01 Strict
    > evangelism: XHTML 1.0 Strict
    > madness: XHTML 1.1 as application/xhtml+xml
    > -- Bjoern Hoehrmann



    Hi Bjoern Hoehrmann, I m sure u haven't tried to run this code, this
    code not just for validating the Phone Number, but it has some more .
    But anways thanks for ur useful comment regarding my "JUNK POST"..
    Keep Posting :)
     
    Abhishek, Aug 7, 2008
    #5
  6. Abhishek

    David Mark Guest

    Re: Javascript to Validate the Phone Number Field Using Javascript

    On Aug 7, 5:07 am, Abhishek <> wrote:
    > On Aug 5, 11:51 am, Thomas 'PointedEars' Lahn <>
    > wrote:
    >
    > > Abhishek wrote:
    > > > Hi this is my another validator in javascript to validate the Phone
    > > > Number :)
    > > > [...]
    > > > Please feel free to give Comments and Suggessions and Bugs also :)

    >
    > > Please stop posting your clueless, invalid junk code (here).  Thanks in advance.

    >
    > > PointedEars
    > > --
    > >     realism:    HTML 4.01 Strict
    > >     evangelism: XHTML 1.0 Strict
    > >     madness:    XHTML 1.1 as application/xhtml+xml
    > >                                                    -- Bjoern Hoehrmann

    >
    > Hi Bjoern Hoehrmann, I m sure u haven't tried to run this code, this


    Oh brother. I'm sure he hasn't either.

    > code not just for validating the  Phone Number, but it has some more .


    How very.

    > But anways thanks for ur useful comment regarding my "JUNK POST"..
    > Keep Posting :)


    Nice attitude. And if you could refrain from posting junk here, that
    would be good too! ;)
     
    David Mark, Aug 7, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ori
    Replies:
    2
    Views:
    21,343
    Brian W
    Jan 26, 2004
  2. Sharon Traineanu via .NET 247

    Single Validator for 3 field Phone Number

    Sharon Traineanu via .NET 247, May 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    642
    Peter Blum
    May 10, 2004
  3. Zahoor
    Replies:
    1
    Views:
    833
    Darryl L. Pierce
    Nov 15, 2003
  4. chen selina
    Replies:
    0
    Views:
    903
    chen selina
    Jul 7, 2010
  5. Rene Ivon Shamberger

    convert Phone Letters to Phone numbers

    Rene Ivon Shamberger, Dec 4, 2012, in forum: C++
    Replies:
    5
    Views:
    431
Loading...

Share This Page