Reputation: 9
A JavaScript program is required to request, via a dialogue box, a password from a user. The input, which can be of any length, must be validated such that it is only valid if it has no spaces. Any character, other than a space, is permissible. As soon as a space is detected, the program should terminate with a message provided in an alert box stating: Invalid, contains a space!
Can anyone help me correct my code, this is my attempt
<!DOCTYPE html>
<html>
<body>
<form action="">
Password: <input type="password" name="password">
</form> ;
var i ;
var validpassword = "" ;
var passworduser ;
var isNoSpace = (?!.*\\s)
if(isNoSpace = false; i > charAt(i)) //
alert("Password contains a space!")
for(j = 1; j<=yourString.length; j++){
validpassword += ’*’;
}
document.write("valid password")
else
document.write("password not valid")
</body>
</html>
Upvotes: 0
Views: 71
Reputation: 2351
Try with var isNoSpace = passworduser == passworduser.replace(" ", "");
.
It will check if the passworduser
string is the same even when spaces are removed. If it is, it will return true (so isNoSpace = true
).
The syntax I have used is equivalent to this
var isNoSpace;
if (passworduser == passworduser.replace(" ", "")) {
isNoSpace = true;
else
isNoSpace = false;
Upvotes: 0
Reputation: 47099
var containingSpace = function(str) {
return str.indexOf(' ') > -1;
}
console.log(containingSpace('abc')); // false
console.log(containingSpace('ab c')); // true
Upvotes: 3
Reputation: 2532
RegEx: /\s+/
var pattern = /\s+/;
if (pattern.test(userString)) {
document.write("password not valid");
} else {
document.write("valid password");
}
Upvotes: 0