If you have a form with a ‘confirm password’ field and you want to match that with a simple Javascript code rather than to use a heavy validation than this solution will helpful for you. In this, 'onkeyup' event was used to match both fields while user entering input. if you want to match it after textbox loses their focus, you have to call it on 'onblur' event, nothing more than this. You have to do something Like this:-
aspx code
<form id="form1" runat="server">
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" ClientIDMode="Static" runat="server"></asp:TextBox>
<asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password"></asp:Label>
<asp:TextBox ID="txtConfirmPassword" ClientIDMode="Static" runat="server" onkeyup="matchPassword();"></asp:TextBox>
</form>
Javascript function
<script type="text/javascript">
function matchPassword() {
//store both values into variables.
var password1 = document.getElementById('txtPassword');
var password2 = document.getElementById('txtConfirmPassword');
//set color to differentiate both conditions.
var matchColor = "#66cc66";
var unmatchColor = "#ff6666";
if (password1.value.trim() == password2.value.trim()) {
//if passwords match.
password2.style.backgroundColor = matchColor;
} else {
//if passwords do not match.
password2.style.backgroundColor = unmatchColor;
}
}
</script>
No comments:
Post a Comment