2013-03-22 75 views
0

需要验证文本框的值,并抛出错误,如果它包含字母,特殊字符或数字小于20jQuery的验证特殊字符的

代码:

<input type="text" class="input-text-bx" id="actual" value=""> 
      <input type="submit" id="real" value="submit"/> 

我有试过jquery 1.3没有点击提交按钮。我需要提交最新的$。

Query(function(){ 

      jQuery('.input-text-bx').validate({ 
       expression: "if (VAL) return true; else return false;", 
       message: "Please enter a valid number" 
      }); 
     jQuery('.input-text-bx').validate({ 
       expression: "if (VAL.match(/^[0-9]*$/)) return true; else return false;", 
       message: "Actual card can only be in numbers" 
      }); 
      jQuery('.input-text-bx').validate({ 
       expression: "if ((VAL.length >=16))return true; else return false;", 
       message: "The card number should be 16-20 digits" 
      }); 
});  
+0

(你有没有试过? – Lemurr 2013-03-22 02:47:36

+0

添加了我试过的 – user2194155 2013-03-22 02:55:02

+0

验证何时?在表单上提交,模糊或...? “抛出错误”是什么意思?如果你明确说明什么会被认为是有效的,这可能会更清楚。 “小于20的数字”开放给几种解释。 – nnnnnn 2013-03-22 02:58:45

回答

0

可以使用onblur事件上的投入

jQuery('.input-text-bx').bind('onblur', function(){ // Validation Here }); 

,并在功能做你检查的长度(> 16 & < 20)。您也可以匹配RegEx。

0

代码中的.validate()(和标记)表示您尝试使用the jQuery Validate plugin。否则,在没有它的情况下,jQuery中不存在.validate()这样的东西。

<input type="text" class="input-text-bx" id="actual" value=""> 

jQuery('.input-text-bx').validate({ 
    expression: "if (VAL) return true; else return false;", 
    message: "Please enter a valid number" 
}); 

1)您绝对不能附加.validate()比一个<form></form>元素之外的任何元素。没有解决方法。 2)没有选项expression。如果您需要编写自定义方法或规则,则必须使用the plugin's built-in addMethod method


尝试使用像这样的插件。 (所有input元素必须有name属性为这个插件才能正常工作)

HTML

<form id="myform"> 
    <input type="text" class="input-text-bx" name="actual" id="actual" /> 
    <input type="submit" /> 
</form> 

jQuery的

$(document).ready(function() { 

    $('#myform').validate({ // initialize the plugin 
     rules: { 
      actual: { 
       required: true,  // required field 
       number: true,   // must be a number 
       rangelength: [16, 20] // must be between 16 and 20 digits 
      } 
     } 
    }); 

}); 

DEMO:http://jsfiddle.net/NvREa/

文档:http://docs.jquery.com/Plugins/Validation

+0

我需要使用什么验证插件来实现它?如何检查特殊字符? – user2194155 2013-03-22 03:23:36

+0

@ user2194155,你用“jQuery Validate”插件标记了问题,你试图使用'.validate()',所以我认为你已经安装了它。按照我的答案中的示例和文档。同样在这里:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ – Sparky 2013-03-22 03:25:09