New course! Every coder should learn Generative AI!
Try a free lesson0
Including if statement
I want my code below to return false and alert the user if they input a username that contains a space. How would you write it? if(username INCLUDES “ ”){ alert(“”) return false; } sorry if the title is phrased oddly, I didn’t know how to say it. https://code.sololearn.com/WtcEEjQV6VOf/?ref=app
4 Answers
+ 1
Time to learn Regular Expression or Regex in short.
https://www.sololearn.com/learn/9704/?ref=app
The username regex is basically like:
if(!username.match(/[a-zA-Z0-9.\-_$@*!]{3,30}$/)){
alert("Your username must not contain spaces. Must be min 3 max 30 character.")
return false;
}
https://stackoverflow.com/questions/15933727/javascript-regular-expression-for-usernames-no-spaces
+ 3
Try to add:
if(username.indexOf(" ")!==-1) {
alert("Username must not contain spacing.");
return false;
}
https://code.sololearn.com/WWK6ulX3vhdX/?ref=app
+ 1
Calviղ Very brilliant soln
0
Try this:
if(username.match(/\s/) != null){
alert("")
return false;
}