javascript 如何使用Jquery客户端或C#服务器端验证Pan卡号格式

ttcibm8c  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(91)

我必须在我的申请中添加Pan卡号。这样格式就像Ex:BNOPS0157F前5个号码。应该是字母,然后4个号码。应该是数字,最后是字母。怎么做

x8diyxa7

x8diyxa71#

使用RegularExpressionValidator来实现尝试这个...

<asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="txtPan"
   ID="regConVal" ValidationExpression="^[A-Za-z]{5}[0-9]{4}[A-Za-z]{1}$" runat="server" ErrorMessage="PAN Number Should be this format 'XXXXX0000X'."
   ForeColor="#ff0000" ValidationGroup="ConVal"></asp:RegularExpressionValidator>

在javascript中

var pan = "ABCDE1234F";
var pan2 = "ASW4578W32"
var patt =/^[A-Za-z]{5}[0-9]{4}[A-Za-z]{1}$/;

console.log(patt.test(pan));
console.log(patt.test(pan2));

或在C#中

using System.Text.RegularExpressions;

string checkPanNo = @"^[A-Za-z]{5}[0-9]{4}[A-Za-z]{1}$";
bool isPANValid = Regex.IsMatch(txtPanNo.Text.ToString().Trim(), checkPanNo);
if (isPANValid == true)
{
     //valid pan number
}
else 
{
    //invalid pan number
}
l0oc07j2

l0oc07j22#

//Use below jQuery for PAN Validation.

$(".txtPANID").keypress(function (e) {
        var textValue = $(this).val().toUpperCase();
        var char = String.fromCharCode(e.which)
        console.log(textValue + char);
        var regexp = /^([A-Z]([A-Z]([A-Z]([A-Z]([A-Z]([0-9]([0-9]([0-9]([0-9]([A-Z])?)?)?)?)?)?)?)?)?)?$/;
        if (!regexp.test(textValue + char))
            e.preventDefault();
        //return false
    });

相关问题