0

图片

请参考上图。我对jquery很陌生,我开发了一个php表单来输入数据。如果他/她输入错误的值,我想限制用户离开文本字段。范围:比如 0 到 50。它也应该接受 AB、NR。

任何想法如何实现这一点。

我非常感谢您的快速回复。

我换了

<script>
$.validator.addMethod("custom_number", function(value, element) {
return this.optional(element) || value == "AB" || value == "NF" || 
value.match(/^[0-9,\+-]+$/);
}, "Please enter a valid number, or 'AB'");

$(document).ready(function(){
$("#form").validate({
rules: {
'hd[]': {
required: false,
custom_number: true   
}
}
});
});
</script>

<script>
var accept_values = array("AB","NR",'01','02','03','04','05','06','07','08','09','10');

function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}

var success = 0;
$("input[type='hd[]']").each(function(){
if(!in_array(accept_values, $(this).val())) {
success = 1;
}
});
if(success == 1){
alert("You have entered an invalid value");
return false;

}
//do success
</script>

希望我能接受 01 到 10 和 'AB' 和 'NR' 但它没有成功。有没有办法提供范围和字母?

4

4 回答 4

2

请根据您的要求/需要进行修改:

function validate(){
    $('.theory').each(function(){           
            if($(this).val() == ''){
                alert('Please enter value.');       
                return false;
            }else{
                if($(this).val() < 0  || $(this).val() > 50  ){
                            alert('Please enter correct value.');               
                            return false;
                     }
            }       
    });

  return true;

}
于 2013-04-01T06:28:41.230 回答
1

希望您的文本框具有相同的 css 类(例如:checkrow)。

Jquery 代码(未经测试)

  $(function(){
    var status =true;
    $('.checkrow').on('blur',function(){
    var valueofrow =$(this).val();
    if(valueofrow =='AB' || valueofrow =='NR'){
    status =true;
    } else{
    status =false;
    }
    if(status == false){
    alert("warning message");
    return false;
    } 
    });
    });
于 2013-04-01T06:27:28.843 回答
0
var accept_values = array("AB","NR");

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

var success = 0;
$("input[type='text']").each(function(){
    if(!in_array(accept_values, $(this).val())) {
        success = 1;
    }
});
if(success == 1){
    alert("You have entered an invalid value");
    return false;
}
//do success
于 2013-04-01T06:22:51.640 回答
0

我尝试使用 jquery 验证器解决您的问题。

检查它是否符合您的要求,或者您想要改变一些东西..

        <script src='
        http://code.jquery.com/jquery-latest.min.js '></script>
        <script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'></script>
        <script>
        $(document).ready(function(){

        $('#frm').validate();

        $(".inp_text").each(function(){
        $(this).rules("add", {
        required: true,
        checkValue: true
         });   
       });

            $.validator.addMethod("checkValue", function(value, element) {
            var response = ((value > 0)&&(value <= 50))||((value == 'AB')||(value =='NR'));
           return response;
           }, "invalid value");

        })

        </script>


        <form id='frm'>
        <input type='text' name='inp_text'   class='inp_text'/><br>
        <input type='text' name='inp_text1' class='inp_text'/><br>
        <input type='submit' name=''/>
        </frm>

请注意,所有输入都应具有相同的类

于 2013-04-01T07:18:59.917 回答