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.
Asked
Active
Viewed 688 times
3 Answers
1
You could do:
$('#txtPhone').keydown(function(event) {
if(!$(this).val().test(/^[\d,\s+]*$/g))
event.preventDefault();
});
Hope this helps. Cheers

Edgar Villegas Alvarado
- 18,204
- 2
- 42
- 61
0
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?

Community
- 1
- 1

Balanivash
- 6,709
- 9
- 32
- 48
-
Thank you for your this link, This is really cool. I got my solution and this is the solution I was looking for. – Pankouri Jul 06 '11 at 09:16
-
If this is the answer, then mark it as the answer, so that anyone else facing the same problem will know which one to use. :) – Balanivash Jul 06 '11 at 09:35
-
I am new to stake overflow, would you please tell me to make it as an answer? I hope I will get your Help In this Question also – Pankouri Jul 06 '11 at 10:05
-
There ll be a tick mark below the vote button of each answer. Just click the one near the answer you feel is correct to accept it. – Balanivash Jul 06 '11 at 10:07
0
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')"

Friz14
- 278
- 3
- 11
-
i think u need to use onkeydown cause it fires before the press so it will prevent the character to be puten in the textbox – Marwan Jul 06 '11 at 08:06
-
-
u maybe right but this will be workin only on IE cause in FF event must be passed in the event it self like onkeypress="fncInputNumericValuesOnly(event);" then catch it in side the function then deal with it – Marwan Jul 06 '11 at 08:13
-
i'm sure its not workin in FF cause there is no event object can be call directly in the js function it must be passed through the function :) u try it – Marwan Jul 06 '11 at 08:20
-
maybe you right XD I'm sorry call this function adding on the top a "getElementById("MyId").value();" – Friz14 Jul 06 '11 at 08:25
-
@Friz14 and @Marwan: Thnanx for your nice arguments. A logical and structured argument can be as good as a tutorial. – Pankouri Jul 06 '11 at 08:32