0

Currently I am trying to write a code for validation for my site contact info, and I am stuck for 3 hours now on a probably some small problem, but I just can't figure it out.

The problem I have appears in second IF element inside else element, I want to make regex search for numbers [/d] and whitespace [/s] in selected string, but my code always sees only one rule, and ignores the other one.

I guess the mistake is that i didn't write it well, but I can't figure out how. Please give me some pointers where am I making mistake.

        if (sFirstname == null || sFirstname == "") {
           alert("First name must be filled out");
           return false;
        }
        else {
              if (/\d/, /\s/i.test(sFirstname)){
                 alert("Only Letters can be used in First name")
                 return false;
              }
              else {
                    alert("true")
                    return true;
              }
             }

3 Answers3

1

There are many small thing I would like to change:

!sFisrtname will go true as long as sFirstname is not falsy ("", 0, null, undefined, ...)

Use else if ... instead of else { if ... }.

The statement /\d/, /\s/i.test(...) will be evaluated to:

 /\d/,
 /\s/i.test(...)

Same as:

var a = /\d/;
var b = /\s/i;
a, (b.test(...))

What you want is properly /[\d\s]/.test(...) which will go true if there is a digit or a space in sFirstname. You might consider changing the logic op-in instead of op-out, eg: /[^a-zA-Z]/.test(...). Allow only a-z and A-Z

I made the function return the error instead of alerting it:

console.log(checkFirstName('John')); // "" (no error)
console.log(checkFirstName('John 42')); // "Only a-z can be used in first name"

This can also be used in an if statement:

var error = checkFirstName('John');
if (error) {
  alert(error);
}
else {
  alert('Everything is fine!');
}

And the function:

function checkFirstName(sFirstname) {
    if (!sFirstname) {
        return 'First name must be filled out';
    }
    else if (/[\d\s]/.test(sFirstname)) {
        return 'Only letters can be used in first name';
    }
    else {
        return "";
    }
}
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • Your regex `[\d\s]` won't work because you're performing a weak check for numbers or white space, which means the user can enter any other characters they like. You've also referenced your variable incorrectly. – BadHorsie Jun 03 '16 at 14:36
  • @BadHorsie In the beginning of the answer I suggest OP should consider changing the regex. The second part I on me, but instead of down voting you should simply make an edit as it's clearly a type-o. – Andreas Louv Jun 03 '16 at 14:38
  • Sorry, downvote was not for typo. It's fine, I don't care enough about this to debate it. OP is not very clear on their requirements anyway. I assume OP means they only wish to allow letters (based on their error message) but is not using poor logic in their regex to represent that. If that's the case, I suggest using regex `[^a-z]`. – BadHorsie Jun 03 '16 at 14:45
  • I am trying to make a good validation tool for myself for every input I could ever use, and use it as a starting point for future. For this particular example, I am trying to have only letters allowed in sFirstname input. I bought a book about JavaScript and from it i learned that this is a good way to make validation, so my knowledge of regex is limited at the moment. I honestly don't see why is my (now corrected code) so poor logic? Function i am making for validation is supposed to be called every time user field is changed , and apply css to input and generate text next to input. –  Jun 03 '16 at 15:11
  • @Psyll1 It's not persay poor logic, but consider your regex: `[\d\s]` this will match digits and spaces. But what about special characters like `?`, `!`, `@`, ..., `ø`, `æ`, `å` and unicode characters. Should they be allowed? – Andreas Louv Jun 03 '16 at 15:18
  • Oh! i didn't even though about those...my bad, you were right, it was poor logic, thanks for advice! one more question, since you obviously have more experience with this than me, if i were to fix this, on a scale of 1/10, is it a good choice to do validation for every input like the example above? –  Jun 03 '16 at 15:32
  • Usually I would have a model which have validation rules for each field in said model. And if I would only ask for fullname and I wouldn't worry to much about validating it since some people don't have first names, some don't have last names, and some have quotes in their names, but remember to escape the input on the server in case [Little Bubby Tables](https://xkcd.com/327/) comes by – Andreas Louv Jun 03 '16 at 15:40
0

Please test it like this in your "if" condition, inside else. This regular expression will test only for alphabetic, not for numeric or blank space.

var regx = /[^a-zA-Z]+/;
regx.test(firstname);
Samir
  • 1,312
  • 1
  • 10
  • 16
0

change your IF statement like below

if (/\d|\s/i.test(sFirstname))
Rishi Tiwari
  • 1,041
  • 1
  • 10
  • 20