Hi,
I have written a javascript code for handling the password functionality. The scenario is: when user wants to change the old password, he cannot fill the same old password again. I have written the code. But even the simple alert of blank textbox is not working. See the code for your reference:
Javascript code:-
<script type="text/javascript">
function ltrim(str) {
var reg
reg = /^\s+/g
return str.replace(reg, "")
}
function rtrim(str) {
var reg
reg = /\s+$/g
return str.replace(reg, "")
}
function trim(str) {
return rtrim(ltrim(str))
}
function validate() {
if (trim(document.getElementById('<%=txtOldPassword.ClientID %>').value) == "") {
alert("Please enter the Old Password")
return false
}
if (trim(document.getElementById('<%=txtNewPassword.ClientID %>').value) == "") {
alert("Please enter the New Password")
return false
}
if (trim(document.getElementById('<%=txtNewPassword2.ClientID %>').value) == "") {
alert("Please enter the Confirm Password")
return false
}
if (trim(document.getElementById('<%=txtNewPassword.ClientID %>').value) != trim(document.getElementById('<%=txtNewPassword2.ClientID %>').value)) {
alert("Password and Confirm Password do not match")
return false
}
if (trim(document.getElementById('<%=txtNewPassword.ClientID %>').value) == trim(document.getElementById('<%=txtOldPassword.ClientID %>').value)) {
alert("New Password cannot be same as Old Password")
return false
}
var passwordLen = trim(document.getElementById('<%=txtNewPassword.ClientID %>').value).length
if (passwordLen < 6) {
alert("Password length must be minimum 6 characters")
return false
}
var str = trim(document.getElementById('<%=txtNewPassword.ClientID %>').value)
var fldNames = new Array("<", ">", "(", ")", "{", "}", "[", "]", "?", "&", "*", ">", "~", "@", "!", "#", "$", "%", "^", "=", "+", "|", "\\", ":", "\"", ",", ";");
var isValid = 0;
for (j = 0; j < str.length; j++) {
for (i = 0; i < fldNames.length; i++) {
if (str.substring(j, j + 1) == fldNames[i]) {
isValid = 1
break
}
}
}
if (isValid == 0) {
alert("Please enter a strong password")
return false
}
isValid == 0;
for (j = 0; j < str.length; j++) {
if (str.substring(j, j + 1) == str.substring(j, j + 1).toUpperCase()) {
isValid = 1
break
}
}
if (isValid == 0) {
alert("Please enter a strong password")
return false
}
}
</script>
aspx code:-
<tr>
<td class="auto-style1">Old Password</td>
<td class="auto-style2">
<asp:TextBox ID="txtOldPassword" runat="server" TextMode="Password" class="txtcareer"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqtxtOldPassword" ControlToValidate="txtOldPassword" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style1">New Password</td>
<td class="auto-style2">
<asp:TextBox ID="txtNewPassword" runat="server" TextMode="Password" class="txtcareer"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqtxtNewPassword" ControlToValidate="txtNewPassword" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cmptxtNewPassword" runat="server" ControlToCompare="txtNewPassword" ControlToValidate="txtNewPassword2" ErrorMessage="Password should be same." Operator="Equal" Type="String"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style1">New Confirm Password</td>
<td class="auto-style2">
<asp:TextBox ID="txtNewPassword2" runat="server" TextMode="Password" class="txtcareer"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqtxtNewPassword2" ControlToValidate="txtNewPassword2" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<asp:Button ID="btnSavePassword" runat="server" Text="Save" OnClick="btnSavePassword_Click" Style="width: 63px;" CausesValidation="true"/></td>
</tr>
Please help, why this is not working