简体   繁体   English

如何使用JQuery验证文本

[英]How Validate Text Using JQuery

I just want to ask on how to validate input text using jQuery 我只想问一下如何使用jQuery验证输入文本

I want to determine if input text has a 'AF' character 我想确定输入文字是否带有'AF'字符

Thank you 谢谢

If you just mean validating that a string has "AF" inside of it, you can simply use String.indexOf , eg 如果您只是想验证字符串中是否包含“ AF”,则可以简单地使用String.indexOf ,例如

var someText = "AFTER";
if (someText.indexOf("AF") >= 0) {
    // Text has "AF" in it...
}

If you want to validate an input field, for instance, you can use jQuery to get the value of the field, and perform the same check afterwards, eg 例如,如果要验证input字段,则可以使用jQuery获取该字段的值,然后再执行相同的检查,例如

var someText = $('#myInput').val();
if (someText.indexOf("AF") >= 0) {
    // Input field has "AF" in it...
}

You can pair this with jQuery's .keyup to validate the text as the user types. 您可以将其与jQuery的.keyup以验证文本是否符合用户类型。 See the fiddle below for an example: 有关示例,请参见下面的小提琴:

https://jsfiddle.net/aoldershaw/xg4cwyjh/1/ https://jsfiddle.net/aoldershaw/xg4cwyjh/1/

Use regExpression 使用regExpression

in these expression to cheak the hole text. 在这些表情中che孔文字。

var yourInput = $('#myInput').val();
//with caseSensitive
var isMatch = yourInput.match(/AF/g);
alert(isMatch); // If return 'null' the text not present.

//withOut caseSensitive
var isMatch = yourInput.match(/AF/gi);
alert(isMatch); // If return 'null' the text not present.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM