Syed Ali wrote on 18 Dec 2003 at Thu, 18 Dec 2003 21:27:37 GMT:
Hello,
I am trying to create a regexp to express non letters and space.
I tried using:
var myreg = new RegExp ("[^\\sA-Za-z]");
However, it is not working.
I don't quite know why that doesn't work, but I'm too tired to think
about it properly anyway. :)
Basically I want to allow only words with letters in a
textfield, space is ok, but no special characters such as $%^ or
numbers such as 1234.
If I use: var myreg = new RegExp ("[^A-Za-z]");
then "Hello" is ok, but not "Hello There" because there is a
space between Hello and There. I want to allow:
Hello
Hello There
Hello there how are you
and I do not want to allow any special characters such as #$%^
or numbers.
The expression, /[^\sa-z]/i.test( string ), should evaluate to true
if any character that is not a letter (either case) or space exists
in 'string'. If it evaluates to false, all of the characters are
valid. For example,
Legal example:
if (/[^\sa-z]/i.test( 'Hello there' )) {
// Illegal characters [not executed]
} else {
// All legal characters [executed]
}
Illegal example:
if (/[^\sa-z]/i.test( 'No-one can beat me' )) {
// Illegal characters [executed: hyphen]
} else {
// All legal characters [not executed]
}
By the way, using literal regular expressions should be more
efficient. Always use a literal when you have a constant pattern.
I did test it, but blame the aforementioned tiredness if I did make
a mistake.
Mike
--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")