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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to enforce Validation

Status
Not open for further replies.

kebele

MIS
Joined
Jul 31, 2006
Messages
107
Location
US
I have a search page with one text box and two dropdowns for the users to make a selection.I would like to enforce the users to make at least one selection from the dropdowns or enter a value to the textbox before doing any searching.Do I have to write javascript for this or is this someting that can be done using validation controls? any help would be appreciated.
 
You can do it with 2 ways:
1. client side (javascript)
2. server side (validation controls) <-- prefer

For your case, i would use a CUSTOM validator.
 
So all i need to do is just write my own function and apply that to the three controls i have? If my thought is wrong , can you give me an example how this can be done.
thanks
 
Using the custom validator, you can have both server- and client- side validation code via OnServerValidate and ClientValidationFunction respectively, thus you get the performance benefits of the javascript methodology plus the security of the server-side code.

In the .aspx:
Code:
<asp:CustomValidator runat="server" id="cstSelectionCheck"
        OnServerValidate="Server_CheckSelections"
        ClientValidationFunction="CheckSelections"
        ErrorMessage="You must make a selection." />

In the code-beside:
Code:
protected void Server_CheckSelections(object sender, ServerValidateEventArgs args)
{
    bool selectionMade = false;
   
    //write code to see if selection is made, set to true if so

    args.IsValid = selectionMade;
}

//*******Upon Clicking a Button or Whatever*********
if( Page.IsValid )
{
     //perform the logic for a valid page
}

On the client:
Code:
<script language="JavaScript">
<!--
  function CheckSelections(sender, args)
  {
    var selectionMade = false;
   
    //write code to see if selection is made, set to true if so

    args.IsValid = selectionMade;
  }
// -->
</script>

This may also help:

 
thank you guys I have gone this far and It is not working for me.It just does not show error msg even if i do not make any selection. any Idea?

<asp:CustomValidator ID="AtLeastOneSelection" runat="server"
ErrorMessage="You must make a selection."
ValidateEmptyText="true" OnServerValidate="AtLeastOneSelection_ServerValidate"
Width="624px">
</asp:CustomValidator></span></div>

mycode behing:

Protected Sub AtLeastOneSelection_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles AtLeastOneSelection.ServerValidate

If (txtVendorName.Text <> "") And (drPur.SelectedIndex <> 0 And drPurM.SelectedIndex <> 0) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
 
Are you checking Page.IsValid before executing whatever logic you want to execute if a selection is made?
 
yes I did that. I am looking at the link you provided to me and it seems that i have to create a user control for my code to work right.right? can i do this without creating a user control? thanks
 
It works fine when i only validate one control like this
Protected Sub AtLeastOneSelection_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles AtLeastOneSelection.ServerValidate

If (drPur.SelectedIndex <> 0 And drPurM.SelectedIndex <> 0) Then
args.IsValid = True
Else
args.IsValid = False
End If


End Sub


but when i want to validat more than one control then It does not work

This one does not work:
Protected Sub AtLeastOneSelection_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles AtLeastOneSelection.ServerValidate

If (drPur.SelectedIndex <> 0 And drPurM.SelectedIndex <> 0) And (txtVendorName.Text <> "") Then
args.IsValid = True
Else
args.IsValid = False
End If


End Sub

any help would be appreciated.
 
I figured out what my problem was.I was using "AND" instead of "or".Once I changed that it worked for me. thanks you all for your tips and help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top