简体   繁体   中英

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

I'm using jQuery Ketch-up plugin to validate my forms.

  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'

//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. To apply validation on select, use id of select input Example:

select input:

html code:

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

js code:

$.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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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