Using indexOf to search for multiple characters

I’d like to add a javascript validation to my password form to ensure that users are using special characters. I validate server side as well, but I’d like to add this on top of things.

So far I’ve found that using indexOf and looking for -1 if the specific character does not exist will allow me to alert the user if they have not used a specific special character. For instance:


[SIZE=2]if (Password.indexOf("@") == -1)[/SIZE]
[SIZE=2]  {alert("The passwords you have selected do not contain the @ symbol.");[/SIZE]
[SIZE=2]  myForm.NewPassword.focus();[/SIZE]
[SIZE=2]  myForm.NewPassword.select();[/SIZE]
[SIZE=2]  myForm.buSubmit.disabled = false;[/SIZE]
[SIZE=2]  myForm.buReset.disabled = false;[/SIZE]
[SIZE=2]  return false}[/SIZE]

However, what if I wanted to search to make sure that the string “Password” contained EITHER the @ symbol OR the # symbol? Either one would qualify the password and should return true, but the lack of either symbol should return false. I’m not sure what the correct syntax for ths OR feature would be.

Thanks!

I’ve made things work, but I’m sure that this can be cleaned up. It seems a bit crufty. Any advice?

var SpecCount;
SpecCount = 0;
if (Password.indexOf(“!”) > -1) {SpecCount ++;}
if (Password.indexOf(“@”) > -1) {SpecCount ++;}
if (Password.indexOf(“#”) > -1) {SpecCount ++;}
if (Password.indexOf(“$”) > -1) {SpecCount ++;}
if (Password.indexOf(“%”) > -1) {SpecCount ++;}
if (Password.indexOf(“^”) > -1) {SpecCount ++;}
if (Password.indexOf(“&”) > -1) {SpecCount ++;}
if (Password.indexOf(“*”) > -1) {SpecCount ++;}
if (Password.indexOf(“(”) > -1) {SpecCount ++;}
if (Password.indexOf(“)”) > -1) {SpecCount ++;}
if (Password.indexOf(“~”) > -1) {SpecCount ++;}
if (Password.indexOf(“`”) > -1) {SpecCount ++;}
if (Password.indexOf(“?”) > -1) {SpecCount ++;}
if (SpecCount < 1)
{alert(“The passwords you have selected do not contain any special characters.”);
myForm.NewPassword.focus();
myForm.NewPassword.select();
myForm.buSubmit.disabled = false;
myForm.buReset.disabled = false;
return false}

Try this.

var SpecCount = 0;
var symbols = "!@#$&#37;^&*()~`?";
for(var i=0,l=symbols.length;i&lt;l;i++){
  if(Password.indexOf(symbols.charAt(i)) &gt; -1) SpecCount++;
}

That doesn’t seem to be enforcing special characters. I’ve beefed up my code since my last post, so I’ve included the updated code here:


[SIZE=2]// Password Complexity[/SIZE]

[SIZE=2]var Symbols = "!@#$%^&*()~`?";[/SIZE]
[SIZE=2]var SpecCount = 0;[/SIZE]
[SIZE=2]var NumCount = 0;[/SIZE]
[SIZE=2]// Counts Number of Upper Case Letters[/SIZE]
[SIZE=2]var UpperCount = Password.replace(/[^A-Z]/g, "").length;[/SIZE]
[SIZE=2]// Counts Number of Lower Case Letters[/SIZE]
[SIZE=2]var LowerCount = Password.replace(/[^a-z]/g, "").length;[/SIZE]
[SIZE=2]// Checks for Special Characters in Password String[/SIZE]
[SIZE=2]for(var i=0,l=Symbols.length;i&lt;l;i++){[/SIZE]
[SIZE=2]  if(Password.indexOf(Symbols.charAt(i)) &gt; -1) SpecCount ++;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]// Checks for Numbers in Password String[/SIZE]
[SIZE=2]if (Password.indexOf("1") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("2") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("3") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("4") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("5") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("6") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("7") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("8") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("9") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (Password.indexOf("0") &gt; -1) {NumCount ++;}[/SIZE]
[SIZE=2]if (SpecCount &lt; 1)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2] alert(Password.replace(/[^A-Z]/g, "").length);[/SIZE]
[SIZE=2] alert("The passwords you have selected do not contain any special characters.");[/SIZE]
[SIZE=2] myForm.NewPassword.focus();[/SIZE]
[SIZE=2] myForm.NewPassword.select();[/SIZE]
[SIZE=2] myForm.buSubmit.disabled = false;[/SIZE]
[SIZE=2] myForm.buReset.disabled = false;[/SIZE]
[SIZE=2] return false}[/SIZE]
[SIZE=2]if (NumCount &lt; 1)[/SIZE]
[SIZE=2] {alert("The passwords you have selected do not contain any numbers.");[/SIZE]
[SIZE=2] myForm.NewPassword.focus();[/SIZE]
[SIZE=2] myForm.NewPassword.select();[/SIZE]
[SIZE=2] myForm.buSubmit.disabled = false;[/SIZE]
[SIZE=2] myForm.buReset.disabled = false;[/SIZE]
[SIZE=2] return false}[/SIZE]
[SIZE=2]if (UpperCount &lt; 1)[/SIZE]
[SIZE=2] {alert("The passwords you have selected do not contain any upper case letters.");[/SIZE]
[SIZE=2] myForm.NewPassword.focus();[/SIZE]
[SIZE=2] myForm.NewPassword.select();[/SIZE]
[SIZE=2] myForm.buSubmit.disabled = false;[/SIZE]
[SIZE=2] myForm.buReset.disabled = false;[/SIZE]
[SIZE=2] return false}[/SIZE]
[SIZE=2]if (LowerCount &lt; 1)[/SIZE]
[SIZE=2] {alert("The passwords you have selected do not contain any lower case letters.");[/SIZE]
[SIZE=2] myForm.NewPassword.focus();[/SIZE]
[SIZE=2] myForm.NewPassword.select();[/SIZE]
[SIZE=2] myForm.buSubmit.disabled = false;[/SIZE]
[SIZE=2] myForm.buReset.disabled = false;[/SIZE]
[SIZE=2] return false}[/SIZE]
[SIZE=2]}[/SIZE]

Sorry. Apparently it wasn’t as obvious as I thought.

The code I posted is the equivalent of all but the last conditional in your second post. You need to put that conditional after the code I posted for it to work.

GOT IT!

Thanks.