ASP.NET validators alignment issue - asp.net

I am developing contactus webpage which have a input field called Email. It is validated against a required field validator and regular expression validator with appropriate messages.
Required: Enter Email
Regular Expression: Invalid Email
I am setting these two as given below:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<font color="#FF0000">*</font>
<asp:RequiredFieldValidator ID="rfvemail" CssClass="error_text" ControlToValidate="txtEmail"
runat="server" ErrorMessage="Enter email address."></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revemail" runat="server" ControlToValidate="txtEmail"
ErrorMessage="Invalid Email" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
My problem is both Enter Email and Invalid Email is occupying its own space. For Ex: If I leave email as empty space and press submit, Enter Email is displaying right next to it. If I enter invalid email(xxx), Enter Email is off but taking the space, Invalid Email message is displayed after these space taken by 'Enter Email' before.
Is there any way to remove this space??
Mahesh

Set Display = "Dynamic" on it.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.display%28v=VS.100%29.aspx

Use Diplay = "Dynamic"
The display behavior for the validation control. Legal values are:
None (the control is not displayed. Used to show the error message only in the
ValidationSummary control)
Static (the control displays an error message if validation fails. Space is
reserved on the page for the message even if the input passes validation.
Dynamic (the control displays an error message if validation fails. Space is not
reserved on the page for the message if the input passes validation

If I understand the question correctly, I think the answer is to set the Display property to Dynamic.
If you're using ASP.NET Themes, you can set this as the default for all validators in your Theme using a Skin file, so you never have to worry about it again.

Related

Avoid double clicking submit with validator display set to dynamic

I have some ASP.NET CustomValidators with Display="Dynamic". The layout of the page requires that I use them like this without a ValidationSummary.
<asp:CustomValidator ID="vldFirstThree" ControlToValidate="txtFirstThree" runat="server" OnServerValidate="ValidateTextBox" ValidateEmptyText="true" ErrorMessage="Please enter the first three characters of the owner name.<br />" CssClass="error" Display="Dynamic" Enabled="true"/>
If the user leaves the field blank and clicks submit, the error text is displayed. If the user fixes the problem and then clicks submit again, the validation error text clears, but a second click is required to submit the page. I've tried writing some server-side code and some JavaScript to cause the validated TextBox to blur() before submitting, but I'm still running into the need to click twice: once to clear the message and again to submit.
Is there a way to clear the error text and perform the submit with one click?
The answer is the second one here: RequiredFieldValidator have to click twice.
I had to set EnableClientScript="false".

How to show regular expression validation's message after button click only?

Below is my html code. I have a email textbox and there is a login button. I have added a required field validator and regular expression validator for email textbox.
The problem is that when I type some thing in the email textbox browser's auto suggestion shows some list of emails. When I select any of those emails by using down arrow key and enter key it shows the error message for regular expression validation even though email is in proper format.
<asp:RequiredFieldValidator ID="reqValUserName" runat="server"
ErrorMessage="Email is required!"
ControlToValidate="txtUserName"
ValidationGroup="validateCredential"
Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regValUserName" runat="server"
ErrorMessage="Incorrect format!"
ControlToValidate="txtUserName"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="validateCredential"
Display="Static">
</asp:RegularExpressionValidator>
<asp:TextBox ID="txtUserName" runat="server"
TabIndex="1" CssClass="inputCredential" MaxLength="60"
AccessKey="E"
ValidationGroup="validateCredential">
</asp:TextBox>
<asp:Button ID="btnLogin" runat="server" CssClass="btnPrimary"
Text="Login" onclick="btnLogin_Click"
ValidationGroup="validateCredential"/>
In this image as you see if I select the email from the suggestion and press enter it is showing the wrong email validation message.
Can anyone please let me know, how to stop this kind of message display?
If there is any clarification needed regarding the question then please add it as a comment.
You could add the EnableClientValidation="false" attribute to the regex validator so that it only checks the format on the server after the other validators have been passed.
Or follow the advice here:
What determines the order validators fire in?
Also add regular expression validator
for email text box
<asp:RegularExpressionValidator ID="regtxtPrimaryEmail" runat="server" ControlToValidate="txtEmailId"
Display="Dynamic" CssClass="cssVal" ToolTip="Invalid email." ValidationGroup="registration"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
Then it will not submit until the email format correct.
It seems your some controls has autopostback="true" and your these controls are in update panel if not then what you can do is..Remove the display properties of all the validation controls and on btnLogin_Click Event call Validate(); Method.
Also read more In Depth detail on Validators on MSDN
you'll certainly get your answer....
Thanks for all your answers and suggestions.
Below is what I have done after going through all the answers.
<asp:RegularExpressionValidator ID="regValUserName" runat="server"
ErrorMessage="Incorrect format!"
ControlToValidate="txtUserName"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="validateCredential"
Display="Dynamic" EnableClientScript="false">
</asp:RegularExpressionValidator>
As you can see, I have added EnableClientScript="false" so that the error message will not be shown when I type half of email and select from auto suggestion and press enter.
Now the problem was it was always checking for whether entered credentials are correct of not as it was doing validation in server side. So I had some unnecessary code execution and database interaction.
So in click event handler of login button I did following change.
if(Page.IsValid)
{
// My credential check code
}
So the above block of code will run code for checking correctness of entered credentials only if they are in proper format.
But I am still looking for a better answer. This is only a work around. Because when it comes to performance server side validation can never match client side validation. Here I am compromising with usability. As I want user to be notified immediately after he/she enters a wrong formatted email. This can be achieved by using javascript, but I wonder if there is any way we can achieve it using validator controls..
This is happening because the client-side REV is validating on the partial input. For example, in the above illustration, the REV is validating "r" as its input. In order to verify this,
type in the entire email address "rupeshn#aol.com" >> then
select the suggested email using the down arrow >> then
hit the enter key.
The REV will not complain this time.
As for the solution: implement the REV in javascript. Add a label next to the textbox for error message. Call the js when the cursor exits the textbox. If the validation fails, find the label in the js and add the error message.
Just see the properties of the validator there you will find 'Display' property under Appearance section, set it to dynamic and VOLA!!

Server side validation

I am having an issue with the requiredfieldvalidator control not working on an ASP.net page. I have completed the attributes of that field properly, but when I test it, the postback is allowed to happen even if the field in question is blank.
So I want to do server side validation instead. What is the best way to do that? In the event that caused the postback? Also, if I find out the field is blank, how do I get the user back to the screen with all other values they placed on other fields intact and a message saying "This field cannot be blank".
EDIT:
This is the code:
<asp:TextBox ID="fName" TabIndex="1" runat="server" Width="221px" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="FNameRequiredFieldValidator" runat="server" ControlToValidate="fName" InitialValue="" ErrorMessage="Filter Name cannot be blank." ToolTip="Filter Name cannot be blank.">*</asp:RequiredFieldValidator>
You need to provide the markup for your Button / Link control as well.
The 'CausesValidation' attribute is not supposed to be used on TextBox controls.
The button you click needs to have that attribute set to "True".
Please provide that markup and then I can advise on the alternate server side validation.
To enable Client-side Validation, set the EnableClientScript="true" on the RequiredFieldValidator.
You should also always validate on the server side too. But the RequiredFieldValidator doesn't let you do any special-handling server-side. Just check if Page.IsValid(). This will return false if the field is not supplied.
If you want to do custom validation, use a CustomValidator.

RegularExpressionValidation error message not clear after value is fixed

I am using a regular expression validator to validate several fields on an asp.net form. Here is an example:
<asp:RegularExpressionValidator runat="server"
ID=RegularExpressionValidatortxtEMAIL ControlToValidate="txtEmployerEmail"
ErrorMessage="EMail Address - must be valid"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" ></asp:RegularExpressionValidator>
The problem is that after the user enter a invalid value and then corrects it with a valid value, the error message persists until a postback is generated.
Is there any fix for this?
There's nothing wrong with that validator tag - I've just used it in a page and it works fine. You understand that the validator only gets fired when the control loses focus... It could be something else in your page that's causing the problem, perhaps a JavaScript error?

asp.net sanitizing user input

Does asp.net have a built in mechanism that can sanitize all textbox input instead of redirecting to the error page?
I have a textbox input where a user can enter a name, but if they try to enter and <> tags the page automatically throws an error. I just want to handle this error in a user friendly way.
You'll want to look at the AntiXSS library for that. It's a dll so it's easy to drop in and start using it.
The download is at CodePlex.
You can use the ASP.NET RegularExpressionValidator control with a pattern like: ^[^<>]*$
<asp:RegularExpressionValidator ID="rev" runat="server"
ControlToValidate="txtBox"
ErrorMessage="The <> tags are not allowed!"
ValidationExpression="[^<>]*" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
The RequiredFieldValidator is used in conjunction with the RegularExpressionValidator to prevent blank entries. If that textbox is optional, and only needs to be validated when something is entered, then you don't have to use the RequiredFieldValidator.
The benefit of doing it this way is that the error can be handled gracefully and the user can be notified on the same page.
However, if you need to do this for many textboxes and you just want to present something nicer than the error page, you could handle the ValidateRequest error to provide a friendlier message and keep the user on the same page (not just replace it with a custom error page). For more info, check out Kirk Evans' post: Handling ValidateRequest errors within a Page (refer to the section titled Overriding the OnError Method).
Read this for a step-by-step: http://yourtahir.wordpress.com/2008/03/28/aspnet-not-allow-html-in-text-boxserver-error-in-application-a-potentialy-dangerous-requestform-value-was-detected/
You have to do some web.config work.
ASP.net has validation controls
[http://msdn.microsoft.com/en-us/library/7kh55542.aspx][1]
Also there is Mark Down Editor which is a control that strips out html tags etc.

Resources