chinni776
chinni776

Reputation: 165

alphanumeric validation not working in jquery

my code is given below,all the required js files are included.

<?php 
    echo $this->Html->script('jquery');
    echo $this->Html->script('jquery-ui');
    echo $this->Html->script('jquery.validate');
    echo $html->css('jquery-ui');

?>

when i submit the form without giving any value for UserDetailAliasName the form does not submits and validation error message is displayed("this field is required").but if i submit a value like @,# etc the form submits the data. iam using this form to search for username from db.

$(document).ready(function () {
    jQuery.validator.addMethod("alphaNumeric", function (value, element) {
        return this.optional(element) || /^[0-9a-zA-Z]+$/.test(value);
    }, "Username must contain only letters, numbers.");


    $('#UserDetailIndexForm').validate({
        rules: {
            UserDetailAliasName: {
                required: true,
                alphaNumeric: true
            }
        }
    });

});

Upvotes: 2

Views: 11347

Answers (3)

Mohan Kumar
Mohan Kumar

Reputation: 13

            if ($('#element').val() != "value") {
            var a = e.key;
            if (a.length == 1) return /[a-z]|[0-9]|&/i.test(a);
            return true;
        }

Upvotes: 0

asdfpro
asdfpro

Reputation: 11

Include a message below rules too:

    messages: {
    account:{
        required:"Porfavor ingresa tu ID de cuenta",
        minlength:"La cuenta debe tener al menos 6 caracteres",
        alphanumeric:"No se permite que ingreses caracteres invalidos"
    }

that work for me (my local language is spanish)

Upvotes: 1

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

tested and that works.. is required also not working for you?

demo

Upvotes: 0

Related Questions