Вопрос

Following is my view page,

<table class="width_full cellspace" border="0" cellspacing="0" cellpadding="0" style="vertical-align:top">
    <tr>
        <td colspan="3">
            <strong>@Html.Label(Model.objTRSkillGradeLabelEntities.lblBPBlockedfor)</strong>
        </td>
    </tr>
    <tr style="font:11px/20px Arial, Helvetica, sans-serif">
        <td style="width: 33%;">
            AccountId
            <span class="redFont">*</span> :
            <input type="text" id="txtBlkAccountId" />
            <label id="lblAccountId" class="error"> </label>
        </td>
    </tr>
    <tr>
        <td colspan="3" class="textCenter">
            <input class="btnInput" type="submit" value=@Model.objTRSkillGradeLabelEntities.btnBPSubmit onclick="javascript:UpdateTRData();" />
        </td>
    </tr>
</table>
function UpdateTRData() {  
    var errorflag;
    document.getElementById('lblAccountId').innerText = "";

    var BlkAccountIdV = $('#txtBlkAccountId').val();
    if (BlkAccountIdV == "" || BlkAccountIdV == null) {            
        document.getElementById('lblAccountId').innerText = "Enter AccountID";
        errorflag=1;
    } 

    var txtLength = $("#txtBlkAccountId").val().length;
    if (txtLength != 7) {
        document.getElementById('lblAccountId').innerText = "Enter valid AccountID";
        errorflag = 1;
    }

    var i;
    s = txtLength.toString();
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (isNaN(c)) {
            document.getElementById('lblAccountId').innerText = "Enter valid AccountID";
            errorflag = 1;
        }
    }

    if (errorflag == 1) {
        return false;
    }

    var AssociateID = $('#lblAssoID').text();       
    var BlkAccountId = $('#txtBlkAccountId').val();

    $.ajax({                      
        url:"@Url.Action("UpdateBlockDetails", "TravelReady")",             
        data:{            
            strAssociateID: AssociateID,           
            strBlkAccountId: BlkAccountId
        },
        dataType: "json",
        type: "POST",
        error: function(e) {
            alert(e.getException().getMessage());
            alert("An error occurred.");
        },
        success: function(data) { return data; }
    });
}

I've Validated the Account Id textbox to Accept Only 7 digits. This Validation will work after clicking the button. I need to give one more validation like, could not Enter More than 20 Numbers. That is if I enter 21st digit alert box should display. How can I perform this validation using jQuery?

Это было полезно?

Решение

You can use text box maxlength property.

<input type="text" name="txtBlkAccountId" maxlength="20">

OR

You can use JS:

$("#txtBlkAccountId").on('keypress',function(){   
    if($(this).val().length>20){
        alert("error message");
        return false;
       }
});

DEMO

Другие советы

you can use

 <input type="text" id="txtBlkAccountId"  maxlength="20" />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top