shadiadiph Posted February 22, 2009 Share Posted February 22, 2009 I have been trying to use a regex for my name values I only want letters - . , ' and . spaces so I have this so far but I can't seem to get it to allow apostrophes i have tried adding \'\ but it will not allow it? elseif (preg_match("/^[a-zA-Z\.\,\-\s]*$/", $name) ==false) Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted February 22, 2009 Share Posted February 22, 2009 The apostrophe has no special meaning in regex, so it doesn't need to be escaped and nor does the comma. In fact, inside square brackets, the . has so special meaning either. "/^[a-zA-Z,'.\-\s]*$/" You do realise you're allowing the empty string through with this, don't you? Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Author Share Posted February 22, 2009 mm that doesn't work either with ' apostophe?? I am not letting it allow an empty string sorry i didn't post the if statement that preceeds it I just just tried it like you said it still will not allow apostrophes if i type john d'smith it doesn't validate. if ($name=="") { $error[] ="Name is a required field please complete and submit it again."; } elseif (preg_match("/^[a-zA-Z,'.\-\s]*$/", $name) ==false) { $error[] ="Please fill in a correct name using alphabetic characters only"; } Quote Link to comment Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 my first guess would be that its actually failing because slashes aren't allowed, and slashes are being added to the apostrophe on form submit. echo $name out to confirm, and use stripslashes before the regex if so. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 p.s.- your regex is going to allow people to enter in things like: symbols used in names in non-traditional manner -shadiadiph .shadiadiph 'shadiadiph ,shadiadiph shad\s\s\s\sdiph just symbols ,,,,--''..... ............. ------....... 1 or more whitespace or tab \s\s\s\s\s\s\s no length restriction, other than 1+ (which btw, you can change the * to + in your regex to remove that first condition) s sh sdlkadflaskdjfaslkjslkssssssssssssssssssssssssssssssssssssssssssssssslkjlkalasdfj,,,,,,,,--- Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Author Share Posted February 22, 2009 violent crayon you were right about the stripshases i just added that b4 $_Post and now it allows apostrophes and more disturbing is you were right about everything else which means i have to start again Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Author Share Posted February 22, 2009 sorry never done this before so i haven't got a clue Quote Link to comment Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 give examples of the formats you want to allow, min/max length, etc.... The error message in your code says alphabetic chars only so your pattern for that would be: /^[a-zA-Z]+$/ Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Author Share Posted February 22, 2009 Hi violent crayon sorry I had to get some sleep i tried what you suggested it doesn't allow spaces but would be ideal for a username field /^[a-zA-Z]+$/ I am currently looking for the current regexes. alpha: upper or lowercase abc including .-' with spaces but not at allowed at the start alphanumeric: same as above but with numbers too username: same as you suggested above no spaces but including numbers years: only numbers max length 2 no spaces phone: numbers allowing + and - symbols or spaces but not at the start date: dd-mm-yyyy no spaces only numbers and - email I already have this one seems to work /^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$/ Quote Link to comment Share on other sites More sharing options...
.josh Posted February 23, 2009 Share Posted February 23, 2009 alpha: upper or lowercase abc including .-' with spaces but not at allowed at the start You're going to have to be more specific. For instance, you can do this: /^[a-z][-.a-z\s]*$/i This will force at least 1 a-zA-Z at the beginning, and then 0 or more a-zA-Z - . or spaceTAB chars. So, someone can still do: a------ a...--- a..\s\s\s\s\s\s\s...---- You could change it to this: /^(?:[a-z][-.\s]?)+$/i This will make it to where it has to start with a-zA-Z, and anywhere afterwards, you can only have 1 -. or spaceTAB, but even then, stuff like this will be legal: a.a.a.b-c-d e f John.Smith is-a-r.e.t a-r.dlotsofrandomlettersherethereisnolimit So you need to be more specific. Minimum length? Maximum length? How many times allowed to have those special chars? There is no crystal ball inside the computer. It is not psychic. It does not know what your intentions are. Not even gonna bother addressing the rest of your fields, as it would just be the same song and dance as above. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 23, 2009 Author Share Posted February 23, 2009 is there no library that has all these defined already you are right the computer is not psychic but then it is hard to know what the user might be stupid enough to put in. I can't see how any regex statement can ever be perfect? Quote Link to comment 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.