robcrozier Posted February 6, 2008 Share Posted February 6, 2008 Hi everyone, i will first of all make you aware that i am completely useless at regular expressions. What i'm trying to do, and what i hope one of you lot will be able to help me with is to validate a name that is submitted through a form. Basically i want to ensure that the name only contains letters, nothing else. Here's what i currently have if (!preg_match("/[a-zA-Z]/", $var)) Now, it validates to a certain extent, however it allows you to enter things like semi colon's and apostrophe's etc in the middle of words. Can anyone help??? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/89728-solved-regular-expression-help/ Share on other sites More sharing options...
Bauer418 Posted February 6, 2008 Share Posted February 6, 2008 if (!preg_match("/^[a-z]$/i", $var)) { // Name not valid } Here are some others if you need them - Letters and spaces: /^[a-z\s]$/i - Letters, numbers, and spaces: /^[a-z0-9\s]$/i - Anything but spaces: /^[^\s]$/i I'm sure you can see the patterns now. Quote Link to comment https://forums.phpfreaks.com/topic/89728-solved-regular-expression-help/#findComment-459793 Share on other sites More sharing options...
robcrozier Posted February 6, 2008 Author Share Posted February 6, 2008 Thanks for the reply Bauer418. However.. that still doesn't work. i used the code exactly as you displayed it (and yes i did change the $var name to the corresponding one in my file) LOL Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/89728-solved-regular-expression-help/#findComment-459803 Share on other sites More sharing options...
obsidian Posted February 6, 2008 Share Posted February 6, 2008 The suggestion above is very close, but that limits you to a single letter rather than multiples. Here is how you would modify it to work: <?php if (!preg_match('|^[a-z]+$|i', $name)) { // Name not valid } ?> The little "+" symbol makes sure that there are one or more letters provided. Quote Link to comment https://forums.phpfreaks.com/topic/89728-solved-regular-expression-help/#findComment-459808 Share on other sites More sharing options...
robcrozier Posted February 6, 2008 Author Share Posted February 6, 2008 Great! Thanks for your help obsidian! Quote Link to comment https://forums.phpfreaks.com/topic/89728-solved-regular-expression-help/#findComment-459810 Share on other sites More sharing options...
Bauer418 Posted February 13, 2008 Share Posted February 13, 2008 The suggestion above is very close, but that limits you to a single letter rather than multiples. Here is how you would modify it to work: <?php if (!preg_match('|^[a-z]+$|i', $name)) { // Name not valid } ?> The little "+" symbol makes sure that there are one or more letters provided. Haha, my bad...can't believe I didn't catch that. Quote Link to comment https://forums.phpfreaks.com/topic/89728-solved-regular-expression-help/#findComment-465939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.