In an if statement, I want to check if the string has punctuation in it. If it has punctuation, I want an alert message to pop up.
if((/*regex presumably*/.test(rspace))==true||(/*regex presumably*/.test(sspace))==true){
alert('Please enter your guessed answer in the fields provided, without punctuation marks.');
//if only punctuation are entered by the user, an alert message tells them to enter something.
}
Do I need a regex, or is there an automatic method to do this for me? Thanks in advance!
You don't need regex, but that would probably be simplest. It's a straight forward character class, put the characters you want to disallow inside [...]
, e.g.:
if (/[,.?\-]/.test(rspace)) {
// ....
}
Note that within []
, -
is special, so if you want to include it put a backslash before it (as above). Same with backslash, come to that.
Note that you never need to write == true
in a condition. You can if you want, but it serves no purpose from the JavaScript engine's perspective.