Check alphanumeric characters in field [closed]

0

I'm having a validation problem in a text field. I need the field not to allow ALPHANUMERIC characters , this validation must be done with jquery .

    
asked by anonymous 17.10.2014 / 02:55

1 answer

1

You have not given much information about jQuery Validate, although you have explicitly marked the topic with this tag. But you also did not give us any basis for work.

So I have to consider both possibilities.

No jQuery Validate

<input id="field" name="field" />
<button type="button" id="button">OK</button>

$( document ).ready( function() {

    $( '#button' ).click( function( e ) {

        e.preventDefault();

        if( new RegExp( "[a-zA-z0-9]+" ).test( $( '#field' ).val() ) === false ) {
            alert( 'Somente caracteres alfa-numéricos' );
        }
    });
});

Demo

With jQuery Validate:

<form id="form">
    <input id="field" name="field" />
    <button type="button" id="button">OK</button>
</form>

$( document ).ready( function() {

    $( '#form' ).validate({

        rules: {
            field: { pattern: /[a-zA-Z0-9]+/ }
        }
    });
});

Demo

Please note only the additional file being included in the second Fiddle. I do not use jQuery Validate so I do not know if this version I used is the most up to date or not.

See the Regular Expression technique the same for both cases, change only the implementation.

    
17.10.2014 / 03:34