![](/img/trans.png)
[英]Add JQuery validation plugin custom method using param with class name
[英]jQuery Validation plugin custom method using regex
我需要為jQuery Validator創建一個新方法,不知道從哪里開始。
我想檢查輸入的電子郵件是否包含:'@ specificdomain.com'。
但這也是投入的最后一部分。 例如@ specificdomain.comChris不會這樣做。
<script type="text/javascript">
jQuery.validator.addMethod("mustinclude", function(value, element) {
return this.optional(element) || value == ?
}, "must include @specificdomain.com at the end of the text input");
$(document).ready(function(){ .....
到目前為止,我只遇到了值== value.match(),因此這就是我遇到的問題。
干杯克里斯
jQuery.validator.addMethod('matchDomain', function(value, element) {
var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])+$/;
var s2="@allcoles.com";
var optionalValue = this.optional(element);
if (optionalValue) {
return optionalValue;
}
if(regex.test(split[0]) && s2.equals(split[1]))
{
return true;
}
else
{
return false;
}
}, 'Please specify a @allcoles.com email');
var s="abc@specificdomain.com"; OR var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])/;
var s2="@specificdomain.com";
if(regex.test(split[0]) && s2 == split[1])
return true;
else
return false;
以下對我有用:
jQuery.validator.addMethod('matchDomain', function(value, element) {
var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])+$/;
**var s2="allcoles.com";** //The split array is the domain excluding the @
**var optionalValue = this.optional(element);** //This is how other methods in alternativeMethods.js Validator handle this.
**//Debugging - This is useful to see visually what is happening
//alert(split[0]); // Shows the inputted username i.e chris or smokey
//alert(split[1]); // Shows the inputted domain
//alert(regex.test(split[0])); //Shows unfilled inputs problem or bad characters, true if good, false if bad
//alert(s2 == split[1]);** // Shows if the inputted domain matches variable s2, if it does we get a true
if (optionalValue) {
return optionalValue;
}
**if(regex.test(split[0]) && (s2 == split[1]))** // has to be == not equals
{
return true;
}
else
{
return false;
}
}, 'Please specify a @allcoles.com email');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.