Print Page | Close Window

Problem with RequiredFieldValidator

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=14118
Printed Date: 29 March 2026 at 2:09pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Problem with RequiredFieldValidator
Posted By: Misty
Subject: Problem with RequiredFieldValidator
Date Posted: 05 March 2005 at 3:33pm
I decided to use the RequiredFieldValidator control for input validation instead of JavaScript.
 
Code:
<ASP:TextBox id="txtEmail" runat="server">           
<asp:RequiredFieldValidator 
ControlToValidate="txtEmail"                           
Text="Enter Your Email Address!"              
Runat="Server" />
 
I have found out that the above input validation doesn't work in older version of Netscape. As you know, some older browsers will not work with JavaScript. How do I get the input validation above in ASP.Net to work with all browsers including very old versions?



Replies:
Posted By: Mart
Date Posted: 05 March 2005 at 4:44pm
Well in Page_Load you could just use
 
If txtEmail.Trim() = String.Empty Then
Response.Write("<script>alert('Please enter an email address');</script.")
Exit Sub
End If
 
but if it's an email address don't you want to use a regular expressions validator to check its a valid email AND is not null?


Posted By: Misty
Date Posted: 05 March 2005 at 5:14pm
Mart, It didn't work. I got the following error message: Compiler Error Message: BC30456: 'txtEmail' is not a member of 'System.Web.UI.HtmlControls.HtmlForm'.
I used a regular expressions validator for the email field too.
 
Here's my code for this:
 
<asp:RegularExpressionValidator
ControlToValidate="txtEmail"
Text="Invalid Email Address!"
ValidationExpression="\S+@\S+\.\S{2,3}"
Runat="Server" />


Posted By: Misty
Date Posted: 06 March 2005 at 12:56am
Mart,
 
I finally solved my problem. I am just curious. Which do you prefer to use for input validation in ASP.Net: JavaScript or RequiredFieldValidator? I really like the way Required Expressions Validator works with email addresses. I wish that there was a way to force an user to re-enter the email address if it didn't exist. I have had several people who entered wrong email addresses in one of my forms before. It was really frustrating because I had to call them to find out the correct email address.
 
Here's the code that I put under Page Load:
 
If Page.IsPostBack = True Then
 
  'Check that all of the validation controls 
 'on this page indicate valid input
If Page.IsValid Then
'Add the data as a new record in the database 
Call AddValuesToDatabase()
      
Call AddValuesToEmail()
'Display the results "page" 
Call DisplayResults()
End If
End If
 


Posted By: Mart
Date Posted: 06 March 2005 at 3:45am
I don't prefer any of them, they all have their uses.
 
But to stop people entering the wrong email address why don't you send them an automated email to the address they entered with a verification code or something.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net