I am looking to create Regex for the first name which can allow all the special characters except @()&
, I am trying to implement it in PHP i tried something like
/^[^0-9\@\(\)\&][a-zA-Z\s]*$/
but its not validating properly .

- 188,624
- 52
- 326
- 405

- 13
- 3
-
2What do you mean it's not validating properly? Give us some sample inputs and expected outputs – Explosion Pills Jan 23 '13 at 15:02
-
2Also, are you sure you need/want to validate peoples first names? Have a look at http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Jens Jan 23 '13 at 15:03
-
1How about simply `/^[^@()&]+$/`? – Ja͢ck Jan 23 '13 at 15:05
-
You regex doesn't allow "all the special characters except..." - it doesn't allow any characters except vanilla a-z and A-Z. – Floris Jan 23 '13 at 15:05
-
What's special about those particular characters that you want to excude them and not others like `?` or `#`, etc? And why shouldn't someone have a name containing brackets? (I'll bet there is someone somewhere that does). – SDC Jan 23 '13 at 15:08
-
@SDC I don't think so, but people definitely have name with other char than only [a-zA-Z\s]. Swedish names pops to my mind: ÁSBJÖRN for example. See http://www.behindthename.com/names/usage/swedish. Please don't filter names that much. – Clement Herreman Jan 23 '13 at 15:13
-
@ClementHerreman - well, quite. And apostrophes and Hyphens are common even in English names. And that's without even thinking about Chinese names, or other character sets. I guess the real lesson is to avoid validating names any more than possible -- see also http://stackoverflow.com/questions/3853346/how-to-validate-human-names-in-cakephp/ – SDC Jan 23 '13 at 15:22
-
I've tested you regex and its working fine. – Abu Romaïssae Jan 23 '13 at 15:05
-
@Floris :- well it does allow other characters i have tested this with # % * ..etc but when it comes to @ theres a problem – user2004353 Jan 24 '13 at 05:16
2 Answers
If your intention was to allow special characters (other than those four) anywhere in the string, then your pattern is wrong.
I'll break down your pattern to walk you through what it does:
^
- The match must begin at the start of a line (or entire string).[^0-9\@\(\)\&]
- Match any single character which is not a number, an@
, a parenthesis, or an ampersand. I'm pretty sure the slashes here are superfluous, by the way. The ones before the@
and&
characters almost certainly are, since those characters aren't ever special inside regexes. The ones before the(
and)
might be needed, since those characters are the subpattern delimiters, but I think they're still unneeded here since they're inside a character class.[a-zA-Z\s]*
- Match any lower or uppercase character between A and Z, or any whitespace character, like a space (this is what\s
does). The*
means you can match as many of these characters as there are in a row, or no characters if none of them exist in this position.$
- The match must end at the end of the line (or entire string).
In short, you're only excluding those four special characters from the first character of your string, but you're exluding all special characters as any character after the first.
If you want to allow any character, except those four, in any position in the string, then you should use this as your pattern:
/^[^0-9@&()]*$/
With all of that said, I think you might be overcomplicating things a bit. It's sort of a matter of opinion, but I try to only use regular expressions when there is no other way to do something, since they can be a bit hard to read (this question is a good example of that).
What I would suggest is that you just use str_replace
to remove the four characters you're disallowing, and check the resultant string against your original input:
if($input === str_replace(array('@', '&', '(', ')'), '', $input) {
// process valid input
} else {
// handle invalid input
}
The str_replace
call will take your original string and replace any value in the search array, array('@', '&', '(', ')')
, and remove it (technically, "replace" it with nothing). If the two strings match after that, then none of the invalid characters were present, and your input is valid.
Since you're using parentheses as items within the array, it might be more readable to separate the elements onto their own lines:
$chars_to_remove = array(
'@',
'&',
'(',
')'
);
if ($input === str_replace($chars_to_replace, '', $input)) {
// process valid input
} else {
// handle invalid input
}

- 20,288
- 6
- 64
- 99
-
Thanks a lot AgentConudrum, nice explaination /^[^0-9@&()]*$/ works perfect – user2004353 Jan 24 '13 at 05:31
-
Love the non-regex approach, as well as the clear breakdown of the error in the original expression. @agentconundrum, you have just raised the bar for all of us. Thank you. – Floris Jan 24 '13 at 12:14
-
Thanks for the compliment @Floris. It's good to know I occasionally write something worthwhile. :) – AgentConundrum Jan 24 '13 at 12:33
FirstName <input type=text name="fname" onblur="first(this)" />
function first(ev) {
var val = ev.value;
if(isNaN(val)) {
for(var i = 0; i < 10; i++) {
if(val.indexOf(i) != -1) {
alert("Enter Only chars");
return false;
}
}
}
else {
alert("Enter Only chars");
}
return true;
}

- 5,141
- 2
- 30
- 41

- 19
- 1