简体   繁体   中英

How to use javascript regex to check for “empty” form fields?

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!

Try this for non-whitespace:

([^\s]*)

Example:

/([^\s])/.test("   A"); //TRUE
/([^\s])/.test("    "); //FALSE
function anyChar(str){
    return /\S+/.test(str);
}

will return false if emty data

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.

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 /[az]/i as this regex will only match if there is at least one alphabetical character.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM