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!
An empty regular expression matches everything.
As mentioned above, you can either use RegExp() or regular expression literal to create a RegEx in JavaScript. const regex1 = /^ab/; const regex2 = new Regexp('/^ab/'); In JavaScript, you can use regular expressions with RegExp() methods: test() and exec() .
Use the test() method to check if a string contains only letters, e.g. /^[a-zA-Z]+$/. test(str) . The test method will return true if the string contains only letters and false otherwise.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With