Form validation problem...

Discussion in 'Javascript' started by Eddie, Sep 5, 2003.

  1. Eddie

    Eddie Guest

    I have a form that's used to sort a series of items. The form has a number
    of text fields. Each text field should contain a number. When the form is
    submitted I would like to do two things:

    1. Verify that only numbers are entered in each text field.
    2. Verify that there are no duplicate entries in any text field.

    So each field should be a unique number. Another thing complicating this is
    that I do not know how many fields I will have on any given form. I am
    building this form dynamically from a database call. So the function would
    need to parse through the form and check each field, without specifying the
    name of the field...

    I realize this may be simple for most of you (I'm hoping...), but I'm WAY
    rusty on my Javascript, having spent the past few years doing mostly
    server-side coding...

    THANKS for your help!!!

    E.
     
    Eddie, Sep 5, 2003
    #1
    1. Advertising

  2. > I have a form that's used to sort a series of items. The form has a number
    > of text fields. Each text field should contain a number. When the form is
    > submitted I would like to do two things:
    >
    > 1. Verify that only numbers are entered in each text field.
    > 2. Verify that there are no duplicate entries in any text field.
    >
    > So each field should be a unique number. Another thing complicating this is
    > that I do not know how many fields I will have on any given form. I am
    > building this form dynamically from a database call. So the function would
    > need to parse through the form and check each field, without specifying the
    > name of the field...


    What class is this for, Eddie? I hope you are taking it pass/fail.
     
    Douglas Crockford, Sep 5, 2003
    #2
    1. Advertising

  3. Eddie

    Eddie Guest

    This isn't for a class. I'm building a small content management system for
    my company's intranet and need to create a form that sorts child nodes.
    I've been doing server-side coding (ASP,PHP,JSP) for a few years, but
    haven't done any JS coding in a LONG time. So please excuse my ignorance...
    Can you help?

    eddie

    "Douglas Crockford" <> wrote in message
    news:bjafur$k7g$...
    > > I have a form that's used to sort a series of items. The form has a

    number
    > > of text fields. Each text field should contain a number. When the form

    is
    > > submitted I would like to do two things:
    > >
    > > 1. Verify that only numbers are entered in each text field.
    > > 2. Verify that there are no duplicate entries in any text field.
    > >
    > > So each field should be a unique number. Another thing complicating

    this is
    > > that I do not know how many fields I will have on any given form. I am
    > > building this form dynamically from a database call. So the function

    would
    > > need to parse through the form and check each field, without specifying

    the
    > > name of the field...

    >
    > What class is this for, Eddie? I hope you are taking it pass/fail.
    >
     
    Eddie, Sep 5, 2003
    #3
  4. Eddie

    kaeli Guest

    In article <20030905130522.213$>,
    enlightened us with...
    > I have a form that's used to sort a series of items. The form has a number
    > of text fields. Each text field should contain a number.


    parseInt

    > When the form is
    > submitted I would like to do two things:
    >
    > 1. Verify that only numbers are entered in each text field.


    ..length

    > 2. Verify that there are no duplicate entries in any text field.


    put in array and look for matches

    > So each field should be a unique number. Another thing complicating this is
    > that I do not know how many fields I will have on any given form.


    loop through form.elements[] array


    -------------------------------------------------
    ~kaeli~
    Press any key to continue or any other key to quit.
    Who is General Failure and why is he reading
    my hard disk?
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    -------------------------------------------------
     
    kaeli, Sep 5, 2003
    #4
  5. Eddie

    Eddie Guest

    Thank you!!

    -eddie

    "kaeli" <> wrote in message
    news:...
    > In article <20030905130522.213$>,
    > enlightened us with...
    > > I have a form that's used to sort a series of items. The form has a

    number
    > > of text fields. Each text field should contain a number.

    >
    > parseInt
    >
    > > When the form is
    > > submitted I would like to do two things:
    > >
    > > 1. Verify that only numbers are entered in each text field.

    >
    > .length
    >
    > > 2. Verify that there are no duplicate entries in any text field.

    >
    > put in array and look for matches
    >
    > > So each field should be a unique number. Another thing complicating

    this is
    > > that I do not know how many fields I will have on any given form.

    >
    > loop through form.elements[] array
    >
    >
    > -------------------------------------------------
    > ~kaeli~
    > Press any key to continue or any other key to quit.
    > Who is General Failure and why is he reading
    > my hard disk?
    > http://www.ipwebdesign.net/wildAtHeart
    > http://www.ipwebdesign.net/kaelisSpace
    > -------------------------------------------------
     
    Eddie, Sep 5, 2003
    #5
  6. Eddie

    kaeli Guest

    In article <20030905133856.190$>,
    enlightened us with...
    > Thank you!!
    >


    You're welcome.
    And since you say it isn't for a class...

    > > > I have a form that's used to sort a series of items. The form has a

    > number
    > > > of text fields. Each text field should contain a number.

    > >


    Here's my validation function.
    function checkNumeric(strObject)
    {
    /* Returns true if the field has all numeric characters, false if
    not.
    You must pass in a input (text) object, not the value. */
    var index;
    var myChar;
    /* add chars to the following string if you want punctuation or
    spaces allowed */
    var allowedChars = "0123456789"

    if (! strObject)
    {
    return false;
    }
    else
    {
    var str = strObject.value;
    }

    for (index = 0; index < str.length; index ++)
    {
    myChar = str.charAt(index);
    if (allowedChars.indexOf(myChar) == -1)
    {
    return false;
    }
    }
    return true;
    }

    > >
    > > loop through form.elements[] array


    x = document.formName.elements.length;
    for (i=0; i<x; i++)
    {
    e = document.formName.elements;
    if (e.type == "text")
    {
    // do something
    }
    }
    -------------------------------------------------
    ~kaeli~
    Press any key to continue or any other key to quit.
    Who is General Failure and why is he reading
    my hard disk?
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    -------------------------------------------------
     
    kaeli, Sep 5, 2003
    #6
  7. JRS: In article <20030905130522.213$>, seen in
    news:comp.lang.javascript, Eddie <> posted at Fri, 5 Sep
    2003 10:05:23 :-
    >I have a form that's used to sort a series of items. The form has a number
    >of text fields. Each text field should contain a number. When the form is
    >submitted I would like to do two things:
    >
    >1. Verify that only numbers are entered in each text field.


    Firstly you must define "number". I presume that you do not mean
    "decimal digit". Can it be signed? Can it have a decimal point, dot or
    comma? Can it have thousands separators, comma or dot? Can it have an
    exponent, as in 3e5? Must it be integer?

    If, as seems probable, it must be a decimal digit string, then check it
    with either of the RegExps /^\d+$/ or /\D/ - though the latter accepts
    an empty string.

    >2. Verify that there are no duplicate entries in any text field.
    >
    >So each field should be a unique number. Another thing complicating this is
    >that I do not know how many fields I will have on any given form.


    Assuming that the number of fields is not ludicrously large :

    (1) Do var Them = [] // possibly superfluous
    (2) Do var T, U // subtle
    (3) For each field found, then do
    T = field.value // or +field.value
    if (Them[T]!=U) ... reject, otherwise
    Them[T] = 0

    Possibly != should be !==; test or think.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Sep 5, 2003
    #7
  8. Dr John Stockton <> writes:

    I agree except for details of style.

    > (1) Do var Them = [] // possibly superfluous


    You don't use that Them is an array, so you might as well just use an
    object (purely to satisfy the K.I.S.S. principle), i.e.,
    var Them = {};
    or
    var Them = new Object(); // for older browsers

    > (2) Do var T, U // subtle


    You use this to make U be the undefined value, most likely for compatability
    with old browsers.

    I would much rather set the global variable "undefined" once an for all:
    window.undefined = window.undefined;
    This is safe if undefined is already defined, and defines it if not.

    > (3) For each field found, then do
    > T = field.value // or +field.value
    > if (Them[T]!=U) ... reject, otherwise
    > Them[T] = 0
    >
    > Possibly != should be !==; test or think.


    Change to "Them[T] = true;" and you won't even have to think :)

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 5, 2003
    #8
  9. "Eddie" <> wrote in message
    news:20030905130522.213$...
    > I have a form that's used to sort a series of items. The form has a

    number
    > of text fields. Each text field should contain a number. When the form

    is
    > submitted I would like to do two things:
    >
    > 1. Verify that only numbers are entered in each text field.


    Try regular expressions:

    <script type="text/javascript">
    <!--
    // set up regular expression to accept only numeric expressions made from
    the digits 0 - 9
    numericRE=/^[0-9]+&/;

    function NumericValidate(fieldObject)
    {
    // test to see if contents of field match regular expression using
    RE.test(string) method
    if(!numericRE.test(fieldObject.value))
    {
    alert('you have entered non-numeric characters in a numeric field. Please
    enter numbers only in this field.');
    fieldObject.focus();
    return false;
    }

    // field is numeric
    return true;
    }
    -->
    </script>


    You could call this function from within another function when validating
    the entire form before submitting, or on a 'onblur' event in the field
    itself

    <input type="text" name="accesscode" onblur="NumericValidate(this);">

    > 2. Verify that there are no duplicate entries in any text field.
    >
    > So each field should be a unique number. Another thing complicating this

    is
    > that I do not know how many fields I will have on any given form. I am
    > building this form dynamically from a database call. So the function

    would
    > need to parse through the form and check each field, without specifying

    the
    > name of the field...
    >
    > I realize this may be simple for most of you (I'm hoping...), but I'm WAY
    > rusty on my Javascript, having spent the past few years doing mostly
    > server-side coding...
    >
    > THANKS for your help!!!
    >
    > E.
    >
    >
     
    Richard Hockey, Sep 6, 2003
    #9
    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. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    446
    Tommy
    Nov 29, 2003
  2. Sumith Mathur
    Replies:
    0
    Views:
    2,233
    Sumith Mathur
    Sep 23, 2004
  3. OriginalBrownster

    string validation/ form validation

    OriginalBrownster, Aug 21, 2006, in forum: Python
    Replies:
    1
    Views:
    441
    Simon Forman
    Aug 21, 2006
  4. Grigory Temchenko
    Replies:
    0
    Views:
    341
    Grigory Temchenko
    Dec 28, 2008
  5. bnp
    Replies:
    4
    Views:
    348
Loading...

Share This Page