SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2009
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Regular Expression

    Hi,

    I have been looking online today and found a lot of examples about using regular expressions but I can't get any of them to work.

    I wanted to try creating a basic expression first. I want someone to enter a string that is 2 - 20 characters long and only has letters.

    This is what I use so far, and it doesn't work, nothing happens at all when I run it.

    Code:
    // Check for a valid name.
    var namePattern = new RegExp("/^[a-zA-Z]{2,20}$/");
    if (namePattern.test(name))
    {
    	window.alert("Invalid");
    }
    name comes from a input box I display on screen.

    Also, I would like to update the expression so that there can be one space in the string, but it cannot be at the beginning or end. So this string is like a persons name.

    Bob is OK,
    Bob Smith is also OK.


    Edit: Actually, it is doing something but its always coming back invalid.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2009
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind I found the error.

    Code:
    if (namePattern.test(name) == false)
    {
    	window.alert("Invalid");
    }
    Wow, what a picky syntax, that would have worked in PHP or C# or Java.

    --
    But I'm still not sure about modifying the regex to allow one space in the string as long as its not the first or last character. I can't find any examples of that.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You just left out the ! in the if statement.

    Amending to allow spaces anywhere other than the start or finish is fairly easy. Testing to make sure there is only one space is more easily done by performing two tests.

    Code:
    var namePattern = new RegExp("/^[a-zA-Z][a-zA-Z ]{,18}[a-zA-Z]$/");
    var namePattern2 = new RegExp("/^[a-zA-Z]+ *[a-zA-Z]+$/");
    if (!namePattern.test(name) || !namePattern2.test(name))
    {
    	window.alert("Invalid");
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's not that it's picky, you got it the wrong way round. Originally it should have been:

    Code:
    // Check for a valid name.
    var namePattern = new RegExp("/^[a-zA-Z]{2,20}$/");
    if (!namePattern.test(name))
    {
    	window.alert("Invalid");
    }
    Note the !.

    But I'm still not sure about modifying the regex to allow one space in the string as long as its not the first or last character. I can't find any examples of that.
    Unfortunately demanding that only one space is allowed makes it hard to limit the length. I would just use this simple expression and check the length of "name":

    Code javascript:
    if (name.length > 1 && name.length < 21 && /^([a-zA-Z]+ ?[a-zA-Z]+)$/.test(name)) {
      // ok
    }
    else {
      // invalid
    }
    Note that you don't need to store the regular expression in a variable (unless you intend to use it again, in which case it's a good idea).

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2009
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I originally had the ! operator in there, but it still wasn't working so I removed it to test something else.

    I'm not sure what was wrong but that original code didn't work at all for me. Also, if I remove the "== false" from the statement it stops working again.

    I don't know what causes this.

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thought I would let you know.....

    Validating names is not good practise. You will be telling someone that their name O'riley is invalid. There are also so many other variations of names, some with hyphens, some with characters, single letter names.

    What is thought of as good practise is having no validation of names, but just proper escaping or character replacement. You will then not be telling someone that their name is invalid, or limiting people by their place of origin
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2009
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Markdidj View Post
    Thought I would let you know.....

    Validating names is not good practise. You will be telling someone that their name O'riley is invalid. There are also so many other variations of names, some with hyphens, some with characters, single letter names.

    What is thought of as good practise is having no validation of names, but just proper escaping or character replacement. You will then not be telling someone that their name is invalid, or limiting people by their place of origin
    Yes, but this project is for college and I have to do it the way the professor wants it done, even though it may not be great for a real application. Its suppose to validate to letters only. Its testing how to validate user input is all. Most people are doing it with a simple instr function.

    But he talked about regular expressions the other day and I wanted to try it out.

    He gave us a link to this website and told us we can register here and ask for help on these forums, but we have to post a link to the thread in our project code so he can see what was done.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Validating ALL fields is good practice. It is just a matter of determining appropriate validation. Obviously there are a few characters other than just letters that are valid in names.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    Validating ALL fields is good practice. It is just a matter of determining appropriate validation. Obviously there are a few characters other than just letters that are valid in names.
    That's in the English language. And what about the people who change their name by deed poll.

    Do you think Facebook validates names? I very much doubt it, and they are the most international community website on the net

    Good luck MrGeeker, it's very powerful stuff. Took me a while just to get the hang of the basics, but once you've got it it'll save you loads of lines of code

    As you can make the string safe to use in the rest of your script there is no reason to tell people that their name is invalid.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  10. #10
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    MrGeeker, what would be good is to take a list of random names from Facebook, and choose obscure ones. Test your regexp against those, and test the rest of the class's against them as well.

    There are more characters that exist in legal names than there are that don't. If I wanted, I could change my name LEGALLY to <. That would be 100&#37; valid and legal.

    The only validation you should use on names, imo, is the string length (after being made safe) to make sure it fits in your database.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


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
  •