Jump to content

Recommended Posts

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) 

 

 

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?

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";
}

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,,,,,,,,---

 

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}$/

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.