regex - Jquery validate addMethod validate for custom method -


im using jquery validate , wish add method input text cannot contain ".com"

im tried:

    //.com validator     $.validator.addmethod('dotcom', function(value, element) {         return this.optional(element) || (!value.match(/[.com]/) && value.match(/[.com]/));     },     dotcom); 

but doesn't work, suggestion ? thanks

it looks regex test wrong. try

var dotcom = 'value cannot contain `.com`'  $.validator.addmethod('dotcom', function(value, element) {    return this.optional(element) || !/\.com/.test(value);  }, dotcom);      $('form').validate({    rules: {      site: {        dotcom: true      }    }  })    $('button').click(function() {    $('form').valid();    return false;  })
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>  <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>  <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>    <form>    <div>      <input name="site" />    </div>    <button>save</button>  </form>


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -