Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jquery和无表单验证textboxfor字段?_Jquery_Ajax_Asp.net Mvc_Validation_Unobtrusive Validation - Fatal编程技术网

如何使用jquery和无表单验证textboxfor字段?

如何使用jquery和无表单验证textboxfor字段?,jquery,ajax,asp.net-mvc,validation,unobtrusive-validation,Jquery,Ajax,Asp.net Mvc,Validation,Unobtrusive Validation,我想知道,当我没有表单时,如何使用以下模式激活jquery不引人注目的验证: <script type="text/javascript"> $('.btnAddAsync').click(function (e) { e.preventDefault(); // Avoid href postback var $letter = $(this); var charityAmount =

我想知道,当我没有表单时,如何使用以下模式激活jquery不引人注目的验证:

    <script type="text/javascript">
        $('.btnAddAsync').click(function (e) {
            e.preventDefault(); // Avoid href postback
            var $letter = $(this);

            var charityAmount = $(".charity-gift");

            // *******
            // IF charityAmount IS VALID based on the unobtrusive rule then do the following 
            // *******
            var href = charityAmount.attr('data-href');
            href = href.replace(/(charityGiftAmount=)[0-9]+/ig, '$1' + $(".charity-gift").val());
            ......
        });
    </script>

最后,这里是解决方案。 如果您在服务器端设置了regex规则,那么仍然可以通过jquery以这种方式检索regex模式并手动执行验证

<script type="text/javascript">
$('.btnAddAsync').click(function (e) {
    var charityAmount = $(".charity-gift");

    var validationRule = new RegExp(charityAmount.attr('data-val-regex-pattern'));

    if (validationRule.test(charityAmount.val())) {
       // Regex passed

       var href = charityAmount.attr('data-href');
       href = href.replace(/(charityGiftAmount=)[0-9]+/ig, '$1' + $(".charity-gift").val());
       ......
    }
    else {
       // Regex failed
       ......
    }
});

$('.btnAddAsync')。单击(函数(e){
var charityAmount=$(“.charity gift”);
var validationRule=new RegExp(charityAmount.attr('data-val-regex-pattern');
if(validationRule.test(charityAmount.val())){
//正则表达式通过
var href=charityAmount.attr('data-href');
href=href.replace(/(charityGiftAmount=)[0-9]+/ig,$1'+$(“.charity gift”).val();
......
}
否则{
//正则表达式失败
......
}
});

您需要什么样的验证?您是否只想知道它是否已填写?看起来您需要了解慈善礼物的价值。然后您需要更改
var charityAmount=$(“.charitygift”)
to
var charityAmount=$(“.charity gift”).val()请查看我的edit@CesarBielich:我想触发正则表达式验证:data val regex pattern=“^(\d+(?:[\.\,]\d{2})\)$”
<input class="charity-gift form-control" data-href="/Checkout/Add?eventSlug=trail-and-donation&amp;eventId=4&amp;skuId=5&amp;quantity=1&amp;charityGiftAmount=0" data-val="true" data-val-regex="Invalid amount" data-val-regex-pattern="^(\d+(?:[\.\,]\d{2})?|)$" id="item_CharityGiftAmount" name="item.CharityGiftAmount" type="text" value="">
<script type="text/javascript">
$('.btnAddAsync').click(function (e) {
    var charityAmount = $(".charity-gift");

    var validationRule = new RegExp(charityAmount.attr('data-val-regex-pattern'));

    if (validationRule.test(charityAmount.val())) {
       // Regex passed

       var href = charityAmount.attr('data-href');
       href = href.replace(/(charityGiftAmount=)[0-9]+/ig, '$1' + $(".charity-gift").val());
       ......
    }
    else {
       // Regex failed
       ......
    }
});