16

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!

2
  • Which validation plugin are you using? If it's bassistance's Validation, it has a JSON property you can pass in. Usage: required: true Commented Feb 12, 2010 at 3:20
  • Hi Alex, this is the plugin page : position-absolute.com/articles/… Commented Feb 12, 2010 at 3:23

6 Answers 6

23

Try this for non-whitespace:

([^\s]*)

Example:

/([^\s])/.test("   A"); //TRUE
/([^\s])/.test("    "); //FALSE
Sign up to request clarification or add additional context in comments.

5 Comments

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...
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 - I'll grab that plugin and test in the morning, check your SO messages at lunch tomorrow, this has me curious.
@Nick : Thank so much for this. And plz take your time. I am not worried about this :)
@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.
12
function anyChar(str){
    return /\S+/.test(str);
}

will return false if emty data

Comments

2

I'm using

/^\s*\S+.*/

which means

  • zero or more whitespace characters (\s*), followed by
  • one or more non-whitespace characters (\S+), followed by
  • anything at all, whitespace or not (.*).

This allows a single word or multiple words. I'm allowing whitespace at the beginning because I know how easy it is to miss a single space at the beginning and be really confused as to why your input isn't allowed :|

The Mozilla Developer Network has a great JavaScript regex page for reference.

Comments

1

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.

Comments

1

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.

Comments

1
/^\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 '-]*$/

2 Comments

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.
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 ]*$/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.