Need help with regular expression and form field check

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

    Need help with regular expression and form field check

    I am a complete beginner in regular expressions and would need some help
    from the group regarding checking a form field.

    I have a field on the form where users enters a text string. The user should
    only be able to enter a string in the format of firstname.lastn ame or
    firstname.initi al.lastname. Firstname/initial/lastname can be any characters
    between a-z and there should be a dot between those. How should the
    javascript look like to check this?

    br,

    Tommy


  • Michael Winter

    #2
    Re: Need help with regular expression and form field check

    On Tue, 21 Sep 2004 18:18:18 +0300, Tommy
    <tommy.rasku@RE MOVETHISrasku.n et> wrote:

    [snip]
    [color=blue]
    > I have a field on the form where users enters a text string. The user
    > should only be able to enter a string in the format of
    > firstname.lastn ame or firstname.initi al.lastname.
    > Firstname/initial/lastname can be any characters between a-z and there
    > should be a dot between those. How should the javascript look like to
    > check this?[/color]

    If you want to check the format, nothing more, then use:

    /^[a-z]+\.[a-z]+(\.[a-z]+)?$/i.test(value)

    which will return true for a match, false otherwise.

    If you want to extract the values, use:

    var r = /^([a-z]+)\.([a-z]+)(\.([a-z]+))?$/i.exec(value);

    if(r) {
    var first, init, last;

    first = r[1];
    if(3 == r.length) {
    last = r[2];
    } else {
    init = r[2];
    last = r[4];
    }
    }

    By the way, I don't think it's a good idea to get the user to enter their
    full name delimited by periods; it's not particularly natural. Two text
    boxes with an optional third would be better.

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Lee

      #3
      Re: Need help with regular expression and form field check

      Tommy said:[color=blue]
      >
      >I am a complete beginner in regular expressions and would need some help
      >from the group regarding checking a form field.
      >
      >I have a field on the form where users enters a text string. The user should
      >only be able to enter a string in the format of firstname.lastn ame or
      >firstname.init ial.lastname. Firstname/initial/lastname can be any characters
      >between a-z and there should be a dot between those. How should the
      >javascript look like to check this?[/color]

      You haven't specified a minimum or maximum length for firstname
      or lastname, so "frank.b", "f.baker" and "f.r.b" are all valid.
      That might be best, since there may be a few people with single
      character names. This does mean that you can't detect when the
      user has entered "initial.lastna me" or "firstname.init ial".

      "Frank.R.Ba ker" is not valid, because you specified a-z. If that
      was an oversight, add an "i" after the ending "/" of the RegExp.

      function validate(str){
      if(-1==str.search(/^[a-z]+(\.[a-z]){0,1}(\.[a-z]+){0,1}$/)){
      alert("\""+str+ "\"\nis not in the correct format.");
      }else{
      alert("ok");
      }
      }

      Comment

      • Michael Winter

        #4
        [Correction] Re: Need help with regular expression and form field check

        After reading Lee's post, I see I made a mistake. Corrections are below.

        On Tue, 21 Sep 2004 16:00:00 GMT, Michael Winter
        <M.Winter@bluey onder.co.invali d> wrote:

        [snip]
        [color=blue]
        > If you want to check the format, nothing more, then use:
        >
        > /^[a-z]+\.[a-z]+(\.[a-z]+)?$/i.test(value)[/color]

        /^[a-z]+(\.[a-z])?\.[a-z]+$/i.test(value)

        [snip]
        [color=blue]
        > If you want to extract the values, use:
        >
        > var r = /^([a-z]+)\.([a-z]+)(\.([a-z]+))?$/i.exec(value);
        >
        > if(r) {
        > var first, init, last;
        >
        > first = r[1];
        > if(3 == r.length) {
        > last = r[2];
        > } else {
        > init = r[2];
        > last = r[4];
        > }
        > }[/color]

        var r = /^([a-z]+)(\.([a-z]))?\.([a-z]+)$/i.exec(value);

        if(r) {
        var first, init, last;

        first = r[1];
        init = r[3];
        last = r[4];
        }

        where init will be an empty string if no inital was entered.

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Ivo

          #5
          Re: Need help with regular expression and form field check

          "Lee" wrote[color=blue]
          > Tommy said:[color=green]
          > >
          > >I am a complete beginner in regular expressions and would need some help
          > >from the group regarding checking a form field.
          > >
          > >I have a field on the form where users enters a text string. The user[/color][/color]
          should[color=blue][color=green]
          > >only be able to enter a string in the format of firstname.lastn ame or
          > >firstname.init ial.lastname. Firstname/initial/lastname can be any[/color][/color]
          characters[color=blue][color=green]
          > >between a-z and there should be a dot between those. How should the
          > >javascript look like to check this?[/color][/color]

          Don't forget the infamous O'Briens of this world. Apostrophes, spaces and
          some other weird characters are all very possible in a name.
          --Iv


          Comment

          Working...