views:

136

answers:

4

Hi All:

I am working on a php+javascript based project and have already made up a mockup page at : my website

I knew how to use javascript or php to check whether a particular field of form is "empty"or not, that is, whether it contains alphanumerical characters other than whitepsace characters(for instance, space, tab and newline).

However, my normal apporach no longer works since the jquery plugin that I am using now relies on regex to validate the fields.

If you go to the third tab(3. Fill up Shipping Info and Make Payment), you can enter something into the Firstname field and it does the check automatically. Fine. However, if you just simply put some space characters there and jump to the next field, well, it still feels okay for that, which is not correct since no one's first name is nothing!

The problem? At the back it has a regex like this :

"noSpecialCaracters":{
                    "regex":"/^[0-9a-zA-Z ]+$/",
                    "alertText":"* No special caracters allowed"},

This would not filter out empty characters.

I searched online and tried my best to make up another regex to match, I tried

"regex":"/^[^]+$/"

for matching non-empty characters, but that will not do...

Can anyone help me out? Many thanks in advance!

+1  A: 

Try this for non-whitespace:

([^\s]*)

Example:

/([^\s])/.test("   A"); //true
/([^\s])/.text("    "); //false
Nick Craver
Hi Nick:I tried[code]"regex":"/^([^\s]*)$/", [/code]and [code]"regex":"/^[^\s]*$/", [/code]but neither works. Sorry I am not pro in regex at all...
Michael Mao
@Michael - Try: `/([^\s])/`
Nick Craver
Hi Nick: Yeah that works for a vanilla javascript. But for the plugin... it still won't do. That's not the regex probelm I think. Probably something wrong in other places. thanks for your help!
Michael Mao
@Michael - I'll grab that plugin and test in the morning, check your SO messages at lunch tomorrow, this has me curious.
Nick Craver
@Nick : Thank so much for this. And plz take your time. I am not worried about this :)
Michael Mao
@Michael - The problem is the plugin fails to re-validate in some cases, not sure the route you want to take on fixing that. I'm using the jquery.validate plugin that's a bit more mature...you can do the same stuff, you'd only need to stick in some errorPlacement code to make it a bit fancier on display.
Nick Craver
A: 

You may want to try wrapping your expression in the following ^\s*(expression)\s*$. Then use the groups to find the "trimmed" matches. This eliminates only trailing or leading whitespace.

You can force the user to enter trimmed text or you can gracefully accept untrimmed input (better) as I find copying and pasting text often leaves some trailing or leading whitespace which the user may be unaware of.

Nick Bedford
A: 
/^\s*[0-9a-zA-Z][0-9a-zA-Z ]*$/

that ensures that at least one character is not whitespace and is of one of the allowed characters.

You may also want to consider other characters like hyphen(-) or apostrophe(') that may also appear in names...

/^\s*[0-9a-zA-Z][0-9a-zA-Z '-]*$/
Charles Ma
Hi Charles: It is true that I can force the user not to enter any leading whitespaces and that would certainly help me in this case. However, I still want to know how to walk around this problem, with the help from you guys, of course.
Michael Mao
well, if you want them to be able to have leading whitespace then this should do it/^\s*[0-9a-zA-Z][0-9a-zA-Z ]*$/
Charles Ma
A: 

To answer your question, the minimal regex is /\S/ which will match as long as there is at least one non-whitespace character.

However, you probably don't want someone to put in a first name of '12345' or '!!!', so it might be better to use /[a-z]/i as this regex will only match if there is at least one alphabetical character.

too much php