1,105,380 Community Members

how to validate multiple control?

Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

i have 3 text box which must be in numeric, but i just want shown only one error message once either one of the text box was incorrect formating.
how can i archieve that?

Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

I would use a ValidationSummary control to display the error message in one place. Then you configure the individual validation controls to suppress their error messages.

Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

I know this method, but it still displaying others error message as well.
But is that other tricks?

Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

I have these two MS bookmarks that I refer to when dealing with asp.net validation. Lots if examples and e plantation on how to set the appropriate properties and values.

Validation Summary Class

Control Validation Error Messages

Member Avatar
ss125
Posting Whiz in Training
200 posts since Jul 2013
Reputation Points: 18 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a suggestion here...

Instead of doing like this, why cant to add a javascript that allows only numbers to be added in the control?

Because it saves the validation time and provides more simplicity...

Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

How to do that? please show me a simplest example tq

Member Avatar
geniusvishal
Junior Poster
183 posts since Jan 2012
Reputation Points: -1 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 
Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

The asp.net validation controls produce the javascript for client side validation. In addition, if you use the validation controls, the input will be checked server side as well, just in case the browser did not support javascript or is javascript was disabled/bypassed.

So if you do this with just regular javascript, you should also still validate server side.

Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks, but it don't seem work for me.
i just want display 1 error message for 3 invalid text box input entered.

Member Avatar
geniusvishal
Junior Poster
183 posts since Jan 2012
Reputation Points: -1 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
1
 

Try this:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Validate Multiple Textbox</title>

    <script type = "text/javascript">
     function isNumeric(keyCode) {
         var r = ((keyCode >= 48 && keyCode <= 57) || keyCode == 8 ||
            (keyCode >= 96 && keyCode <= 105))
         if (!r) {
         document.getElementById('lblMsg').innerHTML = 'Only Number Allowed in the Textboxes!';
//             alert("please enter number");
         }
         else
         {
         document.getElementById('lblMsg').innerHTML = '';
         }
         return r;
     }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" onkeydown = "return isNumeric(event.keyCode);" onpaste = "return false;" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox2" onkeydown = "return isNumeric(event.keyCode);" onpaste = "return false;" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox3" onkeydown = "return isNumeric(event.keyCode);" onpaste = "return false;" runat="server"></asp:TextBox><br />
        <br />
        <asp:Label ID="lblMsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></div>
    </form>
</body>
</html>
Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

Here you go, using asp.net validation controls and summary. The validation error is only shown in the summary and not next to the controls.

I tried to give you some hints, but see if this code helps you.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="TextBox 1 only allows numeric values" ControlToValidate="TextBox1" ValidationExpression="[0-9]+" Display="None"></asp:RegularExpressionValidator><br />

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="TextBox 2 only allows numeric values" ControlToValidate="TextBox2" ValidationExpression="[0-9]+" Display="None"></asp:RegularExpressionValidator><br />

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="TextBox 3 only allows numeric values" ControlToValidate="TextBox3" ValidationExpression="[0-9]+" Display="None"></asp:RegularExpressionValidator><br />

<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />

7ba5af2e670aeb0c95816fc8577c253f

Attachments
Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

is that possible to display beside the text box just need 1 message
For instance, 3 text boxes is empty input, so i only display 1 error message *

possible to doing this?

geniusvishal

Thanks for the code, but it still not working.

Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

For instance, 3 text boxes is empty input, so i only display 1 error message

Not with the validation controls. The sample that geniusvishal (+1 for him on a good job) provided does work, i just tested it.

If you go with a javascript solution, you should still validate server side for the reason i provided above.

Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

but i can't work with geniusvishal solution? Maybe is child-page problem?

Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

Javascript is client side so it does not have to do with master/child pages. Your issue is probably related to the element's ID. Remember I mentioned that in another one of your threads. Use the ClientIDMode property.

Member Avatar
gahhon
Posting Pro
505 posts since Jul 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

i am using ClientIDMode="Static" still cannot work with that onKeyDown property. But the onPaste is working. :/

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article