javascript - jQuery Validation - Add custom callback function, check if a string has a space doesn't working -


this code, checkusername function doesn't working, how should fixit?

<form id="register_form" method="post" action="">     <fieldset>         <legend>info</legend>          <div>             <label>username</label>             <input type="text" name="username" id="username"/>             <input type="submit" id="btnaccess" class="button" value="submit" name="btnaccess"/>     </fieldset> </form> 
$(document).ready(function() {     var validator = $('#register_form').validate({         rules: {             username: {                 required: true,                 minlength: 4,                 checkusername: true             }         },         messages: {             username: {                 required: "no blank",                 minlength: "enter atleast 4 words"             }         }     });      $.validator.addmethod('checkusername', function(value, element) {         return /\s/g.test(value);     },  "error"); }); 

http://jsfiddle.net/nambatre/repqr/

p/s: if want check string has words @, # etc, should do?

you need return true the value true

it should be

$.validator.addmethod('checkusername', function(value, element) {     return this.optional(element) || !/\s/g.test(value); }, "error"); 

demo: fiddle


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -