Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom validator and server side code

Status
Not open for further replies.

shaqtus

Technical User
Aug 29, 2010
8
US
I have a form that requires the input of text (such as name, email) and the input of numbers as well (such as salary). For fields that require a number, I need to use a custom validator to verify that the input was indeed a number and not random text. At the same time, when the submit button is pressed, I also need to submit the form data to a database.

The problem I'm having is that I can not get my server side code (to submit to database) to work in conjunction with the custom validator (which uses javascript). In other words, when the "OnClick" event is specified for the submit button, the custom validator does not fire. When the "OnClick" event is left empty for the button, the custom validator does fire. How do I fix this?

Code:
<script type="text/javascript" language="jscript">
function CheckInt(source, args){
    if (isNaN(document.getElementById("<%=txtSalary.ClientID%>").value)) {
        alert('Please enter valid number');     args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
           }
</script>

Code:
<tr>
    <td><b>Salary:</b></td>
        <td>
            <asp:TextBox ID="txtSalary" runat="server" Width="210px" ValidationGroup="qualify"></asp:TextBox>
        </td>
        <td>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtSalary" ErrorMessage="Valid number required" ValidationGroup="qualify" ClientValidationFunction="IntCheck" Display="Dynamic" 
Font-Bold="True" ForeColor="Red">      
            </asp:CustomValidator>
        </td>
</tr>
    <tr>
        <td></td>
        <td style="padding-top:10px;">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                ValidationGroup="qualify" onclick="btnSubmit_Click" /></td>
    </tr>
 
I don't know exactly what your problem is.
What I would do is get rid of the custom validator altogether and use a compare validator to validate that the input is numeric.
 
How would I use a CompareValidator to check if an input is numeric? I am not comparing the input to any other control.

I am kind of new to this, sorry.
 
Scratch that. I figured it out. Thanks for the help! On a side note, I also figured out what my initial problem was. I forgot to wrap my database code in if (Page.IsValid)
 
Ahh OK that makes sense then. But without you showing the code, it's hard to help that much.
Glad you found it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top