Including if statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

19th Nov 2018, 2:00 AM
Yosharu
Yosharu - avatar
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
19th Nov 2018, 2:13 AM
Niush
Niush - avatar
+ 3
Try to add: if(username.indexOf(" ")!==-1) { alert("Username must not contain spacing."); return false; } https://code.sololearn.com/WWK6ulX3vhdX/?ref=app
19th Nov 2018, 2:14 AM
Calviղ
Calviղ - avatar
+ 1
Calviղ Very brilliant soln
19th Nov 2018, 2:20 AM
Dlite
Dlite - avatar
0
Try this: if(username.match(/\s/) != null){ alert("") return false; }
19th Nov 2018, 2:20 AM
Paul
Paul - avatar