Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Validations patterns HTML Forms

Options
  • 03-02-2013 12:09pm
    #1
    Closed Accounts Posts: 3,596 ✭✭✭


    Hi I'm trying to create a form where on the name field the user has to enter letters and spaces, ie Joe Bloggs will be allowed but JoeBloggs will prompt the user to enter the name in the correct format. I can get it to prompt for letters but cant figure out ho to get it to prompt for a spaces, heres what I have:

    <p>Name: <input type="text" id="part" name="part" required pattern="[A-Z]"/><br>

    I have tried [A-Z][" "][A-Z] but that doesn't work


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    You could spend hours fiddling around with these, I'd recommend looking for a regular expressions cheat sheet.
    A working version of what you're trying to do would be:
    pattern="[A-Za-z]+\s[A-Za-z]+"
    But if Brian O'Driscoll or Sharon Ní Bheoláin try to fill out your form, they're going to have a bad time.
    There are so many variations to deal with, not to mention accented characters.
    I'd just leave it and do separate firstname/surname fields, but then I'm tremendously lazy.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    You could spend hours fiddling around with these, I'd recommend looking for a regular expressions cheat sheet.
    A working version of what you're trying to do would be:
    pattern="[A-Za-z]+\s[A-Za-z]+"
    But if Brian O'Driscoll or Sharon Ní Bheoláin try to fill out your form, they're going to have a bad time.
    There are so many variations to deal with, not to mention accented characters.
    I'd just leave it and do separate firstname/surname fields, but then I'm tremendously lazy.

    Cheers, been spending ages trying these out, they are head wrecking, whats does the + on the end mean ?


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    + means match the rule/pattern one or more times.
    So pretty much any number of consecutive letters, followed by a space, followed by any number of consecutive letters.

    You could probably do one that'd work for first-name/surname, but I think it's going to be really long and mind-bending. And even then, you're in a situation where you might not have taken care of every name (and since it's happening so close to the client, you aren't even going to see/log when it's failing). I'd say your best bet would be to search for an existing regex that does what you want. There are a lot of pre-made ones out there for common matches like email addresses, etc.

    The cool thing about regular expressions is that it's a common system, you can use largely the same syntax across multiple languages/environments. So it's worth learning about.
    I wouldn't use this html form as a practice area though, php or javascript would give you better feedback when they aren't working (which they'll do a lot).


  • Registered Users Posts: 2,781 ✭✭✭amen


    a little of topic but don't trust client (html, jscript etc) validation of data.

    You should always test and revalidate on the server sid


  • Registered Users Posts: 6,464 ✭✭✭MOH


    That also won't work for names like O'Shea, or names in other languages with special characters.


  • Advertisement
  • Administrators Posts: 53,752 Admin ✭✭✭✭✭awec


    If you want people to enter their name correctly, have a separate textbox for firstname and surname. If you want to store them as a single field in your database, you can do your concatenation on the server side.

    That's the simplest solution, names really aren't that regular at all. The less regular something is the more difficult it is to create a sensible regex.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    awec wrote: »
    If you want people to enter their name correctly, have a separate textbox for firstname and surname. If you want to store them as a single field in your database, you can do your concatenation on the server side.

    That's the simplest solution, names really aren't that regular at all. The less regular something is the more difficult it is to create a sensible regex.

    Do what awec says, the rest is just insanity.


Advertisement