numeric checker

Discussion in 'HTML' started by Tom, Jan 8, 2007.

  1. Tom

    Tom Guest

    Being a newbie to JS, I would appreciate some advice. When a visitor
    enters a number into a textbox, I want to check that it is 15 digits long
    and then display a message accordingly and the following script does that
    OK. However, I want also to make sure that only "numbers" are entered and
    no other characters. Again the attached does this but is clumsy to say the
    least! How do I get it to loop back to the input box if it contains a
    non-numeric, before it goes off to check for 15 digits?
    <script>
    var ok = " This chip will work";
    var nook = "This chip will not work";
    function check_input()
    {
    var entry = document.forms.f.textfield.value;
    var length = entry.length
    document.clear();
    res = isNaN(entry) ? "please enter only numbers": "Press OK to continue";
    alert(res);
    document.write("<body bgcolor=\"44eedd\">");
    document.write("<body text=\"000000\">");
    document.write("<B><font size=3 face='arial'>")
    if(length == 15) document.write(ok);
    else
    document.write(nook);
    }
    </script>
    Any help would be greatly appreciated.
    Tom
     
    Tom, Jan 8, 2007
    #1
    1. Advertising

  2. Tom

    cwdjrxyz Guest

    Tom wrote:
    > Being a newbie to JS, I would appreciate some advice. When a visitor
    > enters a number into a textbox, I want to check that it is 15 digits long
    > and then display a message accordingly and the following script does that
    > OK. However, I want also to make sure that only "numbers" are entered and
    > no other characters. Again the attached does this but is clumsy to say the
    > least! How do I get it to loop back to the input box if it contains a
    > non-numeric, before it goes off to check for 15 digits?
    > <script>
    > var ok = " This chip will work";
    > var nook = "This chip will not work";
    > function check_input()
    > {
    > var entry = document.forms.f.textfield.value;
    > var length = entry.length
    > document.clear();
    > res = isNaN(entry) ? "please enter only numbers": "Press OK to continue";
    > alert(res);
    > document.write("<body bgcolor=\"44eedd\">");
    > document.write("<body text=\"000000\">");
    > document.write("<B><font size=3 face='arial'>")
    > if(length == 15) document.write(ok);
    > else
    > document.write(nook);
    > }
    > </script>


    There are people here who use javascript, and some may answer you.
    However I also suggest that you go to the Usenet group
    comp.lang.javascript. They have an extensive FAQ that might be useful
    to you and might help. You are quite right in wanting to check for
    charcter length entered and checking for numbers only also, since there
    are people who will enter nearly anything into a form, including hacker
    scripts. Also server side script such as php sometimes can have
    advantages, especially if some of your site visitors have javascript
    turned off.
     
    cwdjrxyz, Jan 8, 2007
    #2
    1. Advertising

  3. Tom

    Neredbojias Guest

    To further the education of mankind, "Tom" <> vouchsafed:

    > Being a newbie to JS, I would appreciate some advice. When a
    > visitor enters a number into a textbox, I want to check that it is 15
    > digits long and then display a message accordingly and the following
    > script does that OK. However, I want also to make sure that only
    > "numbers" are entered and no other characters. Again the attached
    > does this but is clumsy to say the least! How do I get it to loop
    > back to the input box if it contains a non-numeric, before it goes off
    > to check for 15 digits?

    (snip)
    > Any help would be greatly appreciated.
    > Tom


    The easiest way is a regular expression match check for 15 numeric digits.

    OTTOMH: var.match(/\d\d\\d\d\d\d\d\d\d\d\d\d\d\d\d/);

    or

    var.match(/\d{15}/);

    --
    Neredbojias
    Infinity has its limits.
     
    Neredbojias, Jan 9, 2007
    #3
  4. Tom

    Toby Inkster Guest

    Tom wrote:

    > document.write("<body bgcolor=\"44eedd\">");
    > document.write("<body text=\"000000\">");
    > document.write("<B><font size=3 face='arial'>")


    What in the name of sweet Jesus is that?!

    You're trying to output not one but two *extra* BODY elements. An HTML
    document must contain precisely *one* BODY element. The FONT element is
    deprecated too, though that's the least of your worries.

    Better:

    <script type="text/javascript">
    var ok = "This chip will work";
    var nook = "This chip will not work";

    function check_input()
    {
    var entry = document.forms.f.textfield.value;
    window.alert(empty.match(/\d{15}/) ? ok : nook);
    }
    </script>

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
     
    Toby Inkster, Jan 9, 2007
    #4
    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. Replies:
    5
    Views:
    944
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    826
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    968
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. Pager O Rama

    MSN BLOCK CHECKER-MSN STATUS CHECKER-MSN PROBLEMS

    Pager O Rama, Apr 4, 2006, in forum: ASP General
    Replies:
    0
    Views:
    248
    Pager O Rama
    Apr 4, 2006
  5. Jacob Grover
    Replies:
    5
    Views:
    318
    Jacob Grover
    Jul 18, 2008
Loading...

Share This Page