I have a text box in which the SSN number is inserted by user the TexBox has a default value as 'XX-XX-' and then he enters four digit number after ‘XX-XX-‘ only. I have javascript but when he press tab the text in TexBox i.e ‘XX-XX-‘ is get selected and it Flush all the selected text. I want to restrict it to enter only after ‘XX-XX-‘ eg: XX-XX-1234 how to do?
.aspx page:
<asp:TextBox ID="txtInsuredSSN" runat="server" Text="XX-XX-" MaxLength="10" onkeydown="return validateSSN(event);" />
Javascript:
function validateSSN(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('<%#txtInsuredSSN.ClientID %>');
if (phn.selectionStart < 6)
return false;
//comparing pressed keycodes
if ((keycode < 48 || keycode > 57)) {
if ((keycode > 95 && keycode < 106) && phn.value.length < 10)//if num lock is on
{
return true;
}
else if (keycode = 8 && phn.value.length > 6) { //check for backspace
return true;
}
else {
return false;
}
}
else {
//Condition to check textbox contains ten numbers or not
if (phn.value.length < 10) {
return true;
}
else {
return false;
}
}
}
Plz suggest any solution?
use a label for the hard coded prefix, something like :
you need to verify that regex though.