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.

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

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"/>

2 Comments

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.