views:

138

answers:

4

Hi,

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.

A: 

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
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.
You're right. I have upvoted Jack's answers since his satisfies the requirements.
Esteban Araya
+1  A: 

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

Jason w
A: 

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
+2  A: 

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
thanks...will this soln work across safari,firefox,ie browsers?
I'm guessing it would, doc.getelemntbyid is pretty standard.
Jack Marchetti