Pankouri
Pankouri

Reputation: 676

How Can I make it possible that my Text box will take only numbers and ,(coma) and spaace and + characters using Jquery?

I am very bad at client side scripting. I am trying to use JQuery If possible. I have aspx page that has a web form with id = "formUI". This form contains multiple asp text boxes. One of them is (id=)txtPhone. This text-box will take one or more phone numbers separated by comma.I have link of jqury1.6.1.js and jqueryvalidator plugging in my aspx page. How can I validate this Text field using jquery? I am looking forward for an easy and concrete solution from the talented coders of the community of stake overflow.

Upvotes: 0

Views: 699

Answers (3)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

You could do:

$('#txtPhone').keydown(function(event) {
    if(!$(this).val().test(/^[\d,\s+]*$/g))
       event.preventDefault();
});

Hope this helps. Cheers

Upvotes: 1

Friz14
Friz14

Reputation: 278

of course create this function:

function fncInputNumericValuesOnly()
    {
        if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
        {
            event.returnValue=false;
        }
    }

and call it with:

onkeypress= "fncInputNumericValuesOnly('txtQty')"

Upvotes: 0

Balanivash
Balanivash

Reputation: 6867

Check this. This is a code on making textbox to accept only numbers. You can easily extend it to choose your needs. Also check this :How to allow only numeric (0-9) in html inputbox using jQuery?

Upvotes: 0

Related Questions