SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member etron's Avatar
    Join Date
    Dec 2003
    Location
    kuala lumpur
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form validation ...

    I have this text field in my form for the user's name ... and I'm using javascript to validate the text field ...

    in my javascript, the format for the name is : var nameFormat = /^(\w[^\d\W]+)$/;

    the problem start when I try to have space between, like albert john ... it give me error ... and if I input this albert_john ... it is fine ...

    please point to me what is wrong ... thanx ...

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A space is also a character, not included in the 'word' metacharacter (\w), which represents only alphanumerics & the underscore. The problem with names is - there are so many variations, your validation can only be really basic, like letters only (even this may be too restrictive). That is - unless you want no middle names, or whatever; you didn't mention your criteria.
    Code:
    var nameFormat = /^[a-z ]+$/i; //one or more letters or space
    var nameFormat = /^[a-z]+ [a-z]+$/i; //one or more letters, then space, then one or more letters
    ::: certified wild guess :::

  3. #3
    SitePoint Member etron's Avatar
    Join Date
    Dec 2003
    Location
    kuala lumpur
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm ... I didnt think of that ... maybe I should just check if the name field is empty or not ...

    would that be better?

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Generally speaking, the less strict the better. You don't want to punish someone because they have a name you're not accomodating with your expression.

    Their last name (or first) could have an apostrophe (O'Brien) or a hypenated last name (Romijn-Stamos), or something completely unpredictable.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •