Check alphanumeric characters in field

Asked

Viewed 623 times

0

I have a validation problem in a text field. I need the field does not allow ALPHANUMERIC characters, this validation has to be done obligatorily with jquery.

  • 1

    Can you give more details? You can put something you tried to do?

1 answer

1

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

So I have to consider the two possibilities.

Sem 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

Com 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

Just pay attention to the detail of an additional file being included in the second Fiddle. I don’t use jQuery Validate so I don’t know if this version I used is the most updated or not.

Note that the Regular Expression technique the same for both cases, changes only the implementation.

  • I tested both implementations and it didn’t work. @

  • How did you test? In the first Fiddle, typing anything alphanumeric and clicking the button nothing happens. By placing some symbol, an Alert() appears. In the second Fiddle, typing anything not alphanumeric, with a simple TAB makes appear Invalid format between the text box and the button.

  • i solved my problem with a simple application on Html5 defined the rule of regular expression with Pattern="[a-za-Z0-9 . ]{3,30}". But still thank you!

  • 1

    @Ivanfloripa, your 'solution' goes against what you want: obligatorily with jquery. Bruno’s answer solves your problem by using jquery, as you described.

  • I looked for another solution because I tested it in my application and it didn’t work @papa-charlie

  • @Ivanfloripa, but in the fiddle works, maybe it is a minimal matter of adjustment. If you explain the error that occurred, it will be easier to keep the focus on jQuery as you want.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.