Hi KeeganWatkins,
Here is what the OP (nouky) wrote:
I would like to validate a textbox that numbers cannot be entered
into the text input
That sounds to me like he doesn't want any numbers to be allowed
anywhere within the value.
I'm interested to hear why you think /[^\d]/g is more elegant that !/\d/
If the point is to disallow numbers, you don't need to use the global
flag. As soon as the regex comes across the first number, the test
should return false, right? No sense continuing.
Also, have you tried your regular expression? Paste this into
Firebug: /[^\d]/g.test('I have 3 eggs');
It returns true (when it shouldn't).
I suppose you could do something like this:
/^\D*$/.test(textareaValString)
but I don't see how that is more elegant either.
The only other difference I see between our "solutions" is that you
store the result of the regex test in a variable and use that variable
as the condition. But that part is irrelevant to what I was saying to
Michael.
Am I missing something, or have we interpreted the OP's request
differently?
--Karl
____________
Karl Swedberg
www.englishrules.com
www.learningjquery.com
On Aug 9, 2009, at 2:19 AM, KeeganWatkins wrote:
With all due respect, I think karl's solution is somewhat less than
elegant, and could be improved by refactoring to:
// test for anything but numbers
var isValid = /[^\d]/g.test(textareaValString);
if (isValid) {
// proceed with confidence
} else {
// note to user: no numbers allowed!
}
On Aug 8, 11:13 pm, Karl Swedberg <k...@englishrules.com> wrote:
On Aug 7, 2009, at 2:53 PM, Michael Lawson wrote:
yes, you can grab the value of the textbox and match it against a
regular expression
var reg = new RegExp("/[a-zA-Z]/");
if(reg.test("string"))
{
return "valid";
}
else
{
return "not valid";
}
Hey Michael,
I don't think that regular expression is going to work. If you're
using a regular expression constructor method, as opposed to a
literal, you shouldn't use the slashes. Even without the slashes,
that
test will always return true as long as the string has one upper or
lower case letter from a to z.
The OP wanted to disallow numbers, so this should do it:
if ( !(/\d/).test("string") ) {
return 'valid';} else {
return 'not valid';
}
--Karl
____________
Karl Swedbergwww.englishrules.comwww.learningjquery.com