Javascript field validation

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

    Javascript field validation

    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
  • Dr John Stockton

    #2
    Re: Javascript field validation

    JRS: In article <ee261922.05012 50153.5ce6146a@ posting.google. com>,
    dated Tue, 25 Jan 2005 01:53:28, seen in news:comp.lang. javascript,
    Colin Graham <csgraham74@hot mail.com> posted :
    [color=blue]
    > 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.[/color]

    This checks a single field. Your question is unclear as to whether
    field 1 = 11111 and field 2 = 22222 is good or bad. Checking the
    concatenation of your fields does what you might want.

    function Try(S) {
    Bad = /^(.)(\1)+$/.test(S.value) // 1st character is followed by sames
    if (Bad) { alert('Aaarrrgh !') ; S.focus() }
    return !Bad }

    Note - that accepts having only one character, which is necessarily all
    the same; or none. Consider changing + to *.


    OK = /(.)[^\1])/.test(S.value) // does not work, but
    someone may be able to write a RegExp that tests only
    for an instance of a character followed by a
    different one.

    See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.


    [color=blue]
    >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.[/color]

    In most cases, only if you consider them as probably bad examples - of
    course, one can still learn something from them.


    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

    Comment

    • rh

      #3
      Re: Javascript field validation

      Dr John Stockton wrote:[color=blue]
      > JRS: In article <ee261922.05012 50153.5ce6146a@ posting.google. com>,
      > dated Tue, 25 Jan 2005 01:53:28, seen in news:comp.lang. javascript,[/color]

      <...>
      [color=blue]
      >
      >
      > OK = /(.)[^\1])/.test(S.value) // does not work, but
      > someone may be able to write a RegExp that tests only
      > for an instance of a character followed by a
      > different one.
      >[/color]

      Appears to have an extraneous ")", but I think you're right that a
      backreference cannot be used there.

      This perhaps?:

      | OK = /^(.)(?!\1+$)/.test(S.value)
      <...>

      ../rh

      Comment

      • RobB

        #4
        Re: Javascript field validation

        rh wrote:[color=blue]
        > Dr John Stockton wrote:[color=green]
        > > JRS: In article <ee261922.05012 50153.5ce6146a@ posting.google. com>,
        > > dated Tue, 25 Jan 2005 01:53:28, seen in news:comp.lang. javascript,[/color]
        >
        > <...>
        >[color=green]
        > >
        > >
        > > OK = /(.)[^\1])/.test(S.value) // does not work,[/color][/color]
        but[color=blue][color=green]
        > > someone may be able to write a RegExp that tests[/color][/color]
        only[color=blue][color=green]
        > > for an instance of a character followed by a
        > > different one.
        > >[/color]
        >
        > Appears to have an extraneous ")", but I think you're right that a
        > backreference cannot be used there.[/color]

        Not in a negated character class, anyway.
        [color=blue]
        >
        > This perhaps?:
        >
        > | OK = /^(.)(?!\1+$)/.test(S.value)[/color]

        (snip)

        Why would a zero-width assertion be needed?

        <!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()
        {

        var fchk = {
        'val1' : 'value 1' ,
        'val2' : 'value 2'
        };

        var f, ftop, msg = '';
        for (fid in fchk)
        {
        f = document.getEle mentById(fid);
        f.value = f.value.replace (/\s/g, '');
        if (/^$/.test(f.value))
        {
        msg += fchk[fid] + ': blank\n';
        ftop = ftop || f;
        }
        else if (/^(.)\1+$/.test(f.value))
        {
        msg += fchk[fid] + ': same characters\n';
        ftop = ftop || f;
        }
        }
        if (msg != '')
        {
        var bef = 'Errors noted:\n\n';
        var aft = '\nPlease correct.\n';
        alert(bef + msg + aft);
        if (ftop && ftop.focus)
        {
        ftop.focus();
        ftop.select();
        }
        return false;
        }
        return true;
        }

        </script>
        </head>
        <body>
        <form onsubmit="retur n checkit()">
        <input id="val1" type="text" name="val1" value="" />__value 1
        <br />
        <input id="val2" type="text" name="val2" value="" />__value 2
        <hr />
        <input type="submit" value="done" />
        </form>
        </body>
        </html>

        Comment

        • rh

          #5
          Re: Javascript field validation

          RobB wrote:[color=blue]
          > rh wrote:[color=green]
          > > Dr John Stockton wrote:[color=darkred]
          > > > JRS: In article[/color][/color][/color]
          <ee261922.05012 50153.5ce6146a@ posting.google. com>,[color=blue][color=green][color=darkred]
          > > > dated Tue, 25 Jan 2005 01:53:28, seen in[/color][/color][/color]
          news:comp.lang. javascript,[color=blue][color=green]
          > >
          > > <...>
          > >[color=darkred]
          > > >
          > > >
          > > > OK = /(.)[^\1])/.test(S.value) // does not work,[/color][/color]
          > but[color=green][color=darkred]
          > > > someone may be able to write a RegExp that tests[/color][/color]
          > only[color=green][color=darkred]
          > > > for an instance of a character followed by a
          > > > different one.
          > > >[/color]
          > >
          > > Appears to have an extraneous ")", but I think you're right that a
          > > backreference cannot be used there.[/color]
          >
          > Not in a negated character class, anyway.[/color]

          Nor in a non-negated character class, if I recall the semantics
          correctly.
          [color=blue]
          >[color=green]
          > >
          > > This perhaps?:
          > >
          > > | OK = /^(.)(?!\1+$)/.test(S.value)[/color]
          >
          > (snip)
          >
          > Why would a zero-width assertion be needed?
          >[/color]

          Uh, to make it work? ;-) (Note that it does not fully meet JRS's
          suggestion of providing a test for differing adjacent characters.)
          <..>

          ../rh

          Comment

          Working...