0

我有一个文本框,其中 SSN 号码由用户插入TexBoxdefault value as 'XX-XX-'然后他只在“XX-XX-”之后输入四位数字。我有javascript,但是当他按下标签时,TexBox即'XX-XX-'中的文本被选中,它会刷新所有选定的文本。我想限制它只能在'XX-XX-'之后输入eg: XX-XX-1234怎么办?

.aspx 页面:

<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;
                }
            }
        }

请提出任何解决方案?

4

1 回答 1

0

为硬编码前缀使用标签,例如:

<label>
<span class="frmlabelleftfloat">SSN: xx-xx-</span>
<asp:TextBox ID="Last4OfSSN"  runat="server" ValidationGroup="ssnValidation" MaxLength="4"></asp:TextBox>
<span class="failureNotification">*</span>
<asp:RegularExpressionValidator ID="regexpssn" runat="server" ControlToValidate="Last4OfSSN"
    ValidationGroup="ssnValidation"     Display="Dynamic" ForeColor="Red" ErrorMessage="Should be 4 Digits"
    ValidationExpression="\\d{4}?$">   </asp:RegularExpressionValidator>
</label>

您需要验证该正则表达式。

于 2013-02-08T14:10:31.817 回答