简体   繁体   English

可选字段和下拉框的jQuery番茄酱验证规则

[英]jQuery ketchup validation rule for optional fields and drop-down box

I'm using jQuery Ketch-up plugin to validate my forms. 我正在使用jQuery Ketch-up插件来验证我的表单。

  1. I need to validate a drop down box, Eg: if user select the drop down option 'select' it should fire the error saying 'Please select the language'. 我需要验证一个下拉框,例如:如果用户选择下拉选项“选择”,则应触发错误提示“请选择语言”。

  2. Validate an email field but that email field is not a required one. 验证电子邮件字段,但该电子邮件字段不是必需的。

I tried to validate drop-down box using the following code as per the doc. 我尝试根据文档使用以下代码来验证下拉框。 but it says 'jq.ketchup is undefined' 但它说“ jq.ketchup未定义”

//Drop down validation
var jq = $.noConflict();

jq.ketchup.validation('validateSelect', 'Please select the language', function(form, el, value) 
{
      if(this.contains(value.toLowerCase(), 'select')) 
      {
        return false;
      } 
      else 
      {
        return true;
      }
});

For my 2nd question there is no help on the doc. 对于我的第二个问题,文档没有任何帮助。

jquery-ketchup plugin works properly for text and textarea inputs. jquery-ketchup插件可正确用于文本和textarea输入。 To apply validation on select, use id of select input Example: 要对select应用验证,请使用select输入的id示例:

select input: 选择输入:

html code: html代码:

= f.input :type, :prompt => "Select  type",  :collection => (some_collection), input_html: { id: "list_select" }

js code: js代码:

$.ketchup.validation('lselect', 'You have not selected type!', function(form, el, value) {
    return value.length == 0 ? false : true
});

$("#form_id").ketchup({},{
    '#list_select'       : 'lselect'
});

To Validate email only if user entered any value, but not as required field, you need to override the validation provided by plugin as follows: 要仅在用户输入任何值而不是必填字段时验证电子邮件,您需要按如下所示覆盖插件提供的验证:

$.ketchup.validation('email', 'Must be a valid E-Mail.', function(form, el, value) {
    return ($.trim(value).length > 0) ? this.isEmail(value) : true
});

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

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