I think I have this narrowed down to the issue being my JavaScript code. I have this working with an alert but now I want to use my real JavaScript code and its failing. My boss gave me a snippet of some older code that works for a different page/control. So I'm trying to recycle that code and use it here. Anyway I need to do these three things in my JavaScript:
- Password must be at least 8 chars in length
- Password must contain at least one number
- Password must contain at least one capital letter
I have already created a c# version of this function here:
bool hasLength= password1.Length > 7;
bool hasDigit = password1.Any(c => char.IsDigit(c));
bool hasCapital = password1.Any(c => char.IsUpper(c));
if(!hasDigit || !hasCapital || !hasLength)
{
oEr.Number = (int)ErrorClass.ErrorNumber.GeneralError;
oEr.Message = "Password must be at least 8 characters with at least one number, one capital letter and no symbols.";
}
The code above works but its in c# and they want me to write it in JavaScript. Here is what I have currently:
string jsPasswordScript = "onchange() " +
"var pw = document.getElementById('AHA_Password1').value.toString(); "+
"var passw =/^ (?=.*[0 - 9])(?=.*[a - z])(?=.*[A - Z])(?=.{ 8,})/; "+
"var res = passw.test(pw); " +
"if (res) [| label_AHA_Password3.innerText = '';|] "+
"else "+
"[|label_AHA_Password3.innerText='Password must be at least 8 characters with at least one number, one capital letter and no symbols.';"+
" document.getElementById('AHA_Password1').focus();|]];";
oTB.ID = "AHA_Password1";
oTB.TextMode = TextBoxMode.Password;
oTB.TabIndex = 21;
oCell = new HtmlTableCell();
oTB.Attributes.Add("OnChange", jsPasswordScript);
oCell.Controls.Add(oTB);
oRow.Cells.Add(oCell);
Currently the JavaScript is not working. It was working when I used an alert just as a test. So something must be wrong with the JavaScript its self. I'm not super good with JavaScript, can anyone see my mistake in JavaScript or suggest a better route ?
Edit
I updated my JavaScript code. I'm going to test it but here is what I came up with for my new js code.
function passwordvalidation() {
var password = document.getElementById('password1').value.toString();
var hasLength = password.length > 7;
var hasDigit = System.Linq.Enumerable.from(password).any(function(c) {
return System.Char.isDigit(c);
});
var hasCapital = System.Linq.Enumerable.from(password).any(function(c) {
return Bridge.isUpper(c);
});
if (!hasDigit || !hasCapital || !hasLength) {
label_AHA_Password3.innerText = "Password must be at least 8 characters with at least one number, one capital letter and no symbols.";
}
return undefined;
}