开发者

First letter should not be a number using jquery

开发者 https://www.devze.com 2022-12-11 01:50 出处:网络
I am new to jquery and creating login form.One of the condition for the \'user name\' is that, the first letter shou开发者_如何转开发ld not start with a number.

I am new to jquery and creating login form.One of the condition for the 'user name' is that, the first letter shou开发者_如何转开发ld not start with a number. How to do this using jquery?

thanks in advance.


I assume you use jquery validation plugin. On the document ready function add below code.

$("#your-form-id").validate(({
      rules: {
        "userid": {
            required: true,
            uidValid:true// Add custom rule to validate userid field
        }
 }));

// Add custom validation method to validate user id
// Change regular expression according to your need
    $.validator.addMethod("uidValid", function(uid, element) {
        return (this.optional(element) || uid.match(/^[a-z][a-z0-9]*$/i));
    }, "Please specify a valid user id");

If you are not using jquery validation plugin,you can bind an event to onkeypress event of your userid input field ( and on the submit event of the form) and check whether it is match with relevant regular expression. But It will be lot more easier to use validation plugin than manually coding it.

0

精彩评论

暂无评论...
验证码 换一张
取 消