0

I've an asp.net page having a server side submit button and 2 textboxes which accept only numeric values.Am also using asp.net validation controls.

If the user types in non-numeric data in both the textboxes, how do i show only 1 error message on the page saying: "Only numeric values are allowed." And I want to achieve this without firing a server side event.

Thanks.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
user74042
  • 729
  • 5
  • 14
  • 28

4 Answers4

3

Well obviously you'll need to do this with javascript.

I don't know the exact javascript methods to check if input is numeric, I'm guessing you can use a regex. But you could have a hidden div such as

<div id="numericErrorMessage" class="error" style="display:none;">
        Numeric Values only.
</div>

Then you can do:

if(!IsNumber(text1)  || !IsNumber(text2)) {
   document.getElementById(numericErrorMesage).style.display = 'block';
}

of course this is "pseudocodish" but I think this will work for you once you find the way javascript can check for valid numbers and just place that in the IsNumber function

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
1

You could use jquery validation using the \d regex above. Using jquery you will have more control of the output. It is discussed here.

Community
  • 1
  • 1
jasonmw
  • 1,138
  • 9
  • 20
0

You could use a Regex Validator:

<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                 ControlToValidate="TextBox1"
                 ValidationExpression="\d"
                 Display="Static"
                 ErrorMessage="Only numeric values are allowed."
                 EnableClientScript="True" 
                 runat="server"/>
Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
  • thanks...but if i attach RegularExpressionValidator to each of the textboxes and if user types in non numeric values in both of them, then the error message would be shown twice which I dont want to show on the page.I want to show error message only once. – user74042 Jun 27 '09 at 00:19
  • You're right. I have upvoted Jack's answers since his satisfies the requirements. – Esteban Araya Jun 27 '09 at 03:00
0

I would suggest using a custom validator if you only want one error message. You will have to write your own server and client validation functions, but that is easy enough. Here is a link:

http://msdn.microsoft.com/en-us/library/f5db6z8k(VS.71).aspx

You could also use a compare validator, but then you are going to have to have one for each control, and you will cause two error messages.

Chris Mullins
  • 6,677
  • 2
  • 31
  • 40