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

Validating a textbox / Check for 2 Conditions / CustomValidator

Status
Not open for further replies.

DSect

Programmer
Sep 3, 2001
191
US
Hello - I'm an ASP 3.0 person who's new to .NET and I'm trying to figure out how to do something I do in ASP.

I would like to validate a textbox for 2 conditions: Is textbox empty / null and is the string length over 30 chars.

In ASP, I'd do it like this (using 2 custom functions):

Code:
If IsBlank(request.form("myTextbox")) then
       ErrMsg = ErrMsg & "Blah is a required field and cannot be left blank."
End If
If ValidLength(request.form("myTextbox"), 30)
       ErrMsg = ErrMsg & "Blah is a cannot exceed 30 characters."
End If

If ErrMsg <> "" then
  ' It failed validation - write out ErrMsg
Else
  ' Passed validation - do stuff
End If


Ok -

So here's my question:

Can I take advantage of the CustomValidator control to do this exact thing w/ some additional goodness??

The problem I'm having with the CustomValidator control is that the ErrorMessage thing seems to be hard coded. I would like to use my Serverside ValidationFunction to set the error message that is displayed, but it seems I can only have one message.

I hope I'm asking this clear enough.

It seems to me that I would have to add 2 custom validators to check for 2 conditions and have 2 error messages. I would like to have only 1 custom validator for one control that validates for 2 conditions and changes the ErrorMessage to display which conditions were not met.

Thanks again - let me know if you need clarification..
 
Here's an easier way to solve your problem:

1) Add a required validator to your page and set its' ControlToValidate property to your textbox.
2) Set the MaxLength property of your textbox to 30.

That would seem easier from both an end user and implementation standpoint.

Let me know if that helps.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Atomic - That's an idea, but that does not enforce server-side length restrictions.

That's a way to do it, but it's akin to client-side validation because it can be easily circumvented by a smart(er) user / hacker / exploiter.
 
Alright, then...

Whenever I've had to do this, I've used a label instead of the custom validator. Used virtually the same way, but you can easily set the text/other properties on the fly.

Code:
protected bool IsFormValid()
{
   bool _isValid = false;
   if(myTextBox.Length <= 0)
   {
        myErrorLabel.Text = "Required Field";
   }
   else if(myTextBox.Length > 30)
   {
        myErrorLabel.Text = "Too long";
   }
   else{ _isValid = true; }
   myErrorLabel.Visible = (!_isValid);
   return _isValid;
}

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Hmm.. I think what I'm going to have to do is build a "Web Control".

I'm not too familiar with OOP, but I'm hoping that I can use an instance of the CustomValidator Class..

I would like it to participate in the validation the same way that the other field validators do - you know, they bump IsValid to FALSE and also contribute to the validation summary.

Thanks for the info though. The last thing you posted looks like client-side javascript, which I would use but only if it has a server-side counterpart.

Thanks again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top