Javascript Validate Form Fields

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

    Javascript Validate Form Fields

    Hi there,

    Im very new to javascript and this is my first bit of javascript
    coding. I have to write a validation check for two fields that they
    don't contain all the same characters e.g.,

    field 1 = 111111 field 2 = 11111111

    OR

    field 1 = 000000 field 2 = 00000000

    my script wants to check to see if this is happening and display an
    error message.

    I would also be agreatful if anyone could point me towards any
    javascript code banks on the web as this would help speed up my
    learning.

    any help greatly appreciated.

    Thanks

    Colin
  • RobB

    #2
    Re: Javascript Validate Form Fields

    Colin Graham wrote:[color=blue]
    > Hi there,
    >
    > Im very new to javascript and this is my first bit of javascript
    > coding. I have to write a validation check for two fields that they
    > don't contain all the same characters e.g.,
    >
    > field 1 = 111111 field 2 = 11111111
    >
    > OR
    >
    > field 1 = 000000 field 2 = 00000000
    >
    > my script wants to check to see if this is happening and display an
    > error message.
    >
    > I would also be agreatful if anyone could point me towards any
    > javascript code banks on the web as this would help speed up my
    > learning.
    >
    > any help greatly appreciated.
    >
    > Thanks
    >
    > Colin[/color]

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>untitled </title>
    <script type="text/javascript">

    function checkit(els)
    {
    //get field
    var field = els.something;
    //strip whitespace
    field.value = field.value.rep lace(/\s/g, '');
    //empty?
    if (/^$/.test(field.val ue))
    {
    alert('Please enter something.');
    field.focus();
    return false;
    }
    //get first character
    var first = field.value.mat ch(/^./);
    if (first)
    {
    //make it a regular expression
    var re = new RegExp(first[0], 'g');
    //remove all instances of it, test for empty field
    if (/^$/.test(field.val ue.replace(re, '')))
    {
    alert('Entries must not consist of all the same character.');
    field.focus();
    field.select();
    //abort submit
    return false;
    }
    //pro-life
    return true;
    }
    }

    </script>
    </head>
    <body>
    <!-- call validator, pass elements object -->
    <form onsubmit="retur n checkit(this.el ements)">
    <input type="text" name="something " value="" />
    <input type="submit" />
    </form>
    </body>
    </html>

    Studying 'code banks' (imo) will slow down, not speed up your progress.
    You'll need to read some good tutorials/textbooks and progress step by
    step. Here's a good one:


    At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.


    Comment

    Working...