I'm using a FormView control to allow users to insert rows to the database. I want to validate these input fields, and as such have added a regular expression validation helper. Here's the markup:
<InsertItemTemplate>
<p>
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9 ]*$" ControlToValidate="NameTextBox" ID="NameTextBoxValidator" runat="server" ErrorMessage="Must be alphanumeric characters and spaces"></asp:RegularExpressionValidator>
</p>
<p>
Location:
<asp:TextBox ID="LocationTextBox" runat="server"
Text='<%# Bind("Location") %>' />
</p>
<p>
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
</p>
</InsertItemTemplate>
However, when I click InsertButton the page refreshes and I get an error from SQL Server saying it can't insert a NULL value, the validator isn't getting used at all.
How can I fix this?
I assume that the user entered no text and the database does not allow null values.
A RegularExpressionValidator will not validate empty controls. So you need to provide also a RequiredFieldValidator.
The validation will not fail if the input control is empty. Use the
RequiredFieldValidator control to make the field required.
http://www.w3schools.com/aspnet/control_regularexpvalidator.asp
Not much info here, but I'll venture a guess:
Check to make sure you don't have anything happening OnLoad that's blanking things out. If you do have an OnLoad make sure it only fires when IsPostback is false.
Related
I have a form with Required field validator controls to validate if the control is empty or not. I put the CausesValidation="True" in the submit button to prevent the form from saving data if there's required field empty and give an appropriate error message.
However, whenever I click submit, nothing happened: NO error message and the form is not saved either.It just stays there without any event occurring.
Also, i noticed even ALL required fields are correctly filled up, it still does nothing upon submitting, unless if I put the CausesValidation="False", then only I am able to submit the form (but no validations take place, which is not what I want).
here's my code:
My Control to validate:
<asp:TextBox runat="server" ID="TxtCostCenter" Text='<%# Bind("CostCenter") %>' MaxLength="254"
Size="254" SkinID="InputBox" />
<asp:RequiredFieldValidator ID="RfvTxtCostCenter" runat="server" ControlToValidate="TxtCostCenter"
Display="Dynamic" ErrorMessage="<%$ Resources:WebResources, ErrorFieldIsRequired %>" />
also my Submit button (there are two submit buttons though, 1 is at the top and the other is at the bottom of the page):
top:
<asp:LinkButton ID="LinkInsert" runat="server" ToolTip="<%$ Resources:WebResources, ButtonSave %>"
CausesValidation="true" CommandName="Update">
bottom:
<asp:LinkButton ID="LinkButton2" runat="server" ToolTip="<%$ Resources:WebResources, ButtonSave %>"
CausesValidation="true" CommandName="Update">
I also tried the ValidationGroup="val" but it still does not work.
When I am using ValidationSummary to validate my page if I have duplicate errors my validation will show all this errors. I want to display a distinct list of errors. I think that the best approach is to ovverride an event. But I don't know what event to override. What is the event that deals with showing errors.
I don't want solutions for MVC projects!
ValidationSummary collect all the error in your input and display.
So indirectly you have answerd your question by yourself in your question
You just don't know the syntax i think.Here it is:
If you have some collection of input in aspx,you have defined also the regular expression for specific input.For example:
<div>
<asp:TextBox ID="txt" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt" ControlToValidate="txt" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:TextBox ID="txt1" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt1" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt1" ControlToValidate="txtEmail" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Save" ValidationGroup="Submit" OnClick="btnSubmit_Click" />
</div>
<div>
<asp:ValidationSummary ID="ValidationSummary"runat="server"ValidationGroup="Submit" />
</div>
So in every input or button you have to define ValidationGroup attribute
Else if you want to check in codebehind if all of this input are validated you have to do this:
if(Page.IsValid)
{
//your code here
}
In my aspx page:
...
<tr>
<asp:Label ID="FailureText" runat="server" ForeColor="#CC3300"></asp:Label>
<asp:ValidationSummary ID="Alert" runat="server" CssClass="failureNotification" HeaderText=""/>
<tr/>
<tr>
<FTB:FreeTextBox id="FTB" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FTB"
CssClass="failureNotification" ErrorMessage="Content cannot be empty." ToolTip="Content cannot be empty." ></asp:RequiredFieldValidator>
<tr/>
The first time, the code works fine when I let the FTB empty --> FailureText="Content cannot be empty.";
The 2nd time, I press space to input many spaces in FTB --> FailureText doesnt show and the program does the next codes.
I have used RequiredFieldValidator control before but it worked fine for both null or space value.
Help! I really dont know why the RequiredFieldValidator accept space value here???
Maybe this will help. Its not exactly the same, but it sounds like you need to check if only spaces have been entered.
validation on textbox (no space)
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txtBox"
ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
edit...
If you are ok with doing some work server side, this would be an easier solution...
if(string.IsNullOrWhiteSpace(Textbox1.Text))
{
lblError.Text ="Enter required field";
}
Can you please explain why empty value inserted in sql server on asp.net
with the validations(eg: required field validator) provided?
Though the sql table column types is varchar, so in stored procedures the parameters also defined as varchar but not null.
But for some records these fields are inserted as empty value (Not 'NULL')
Added from comment below
<telerik:RadTextBox ID="txtFirstName" runat="server" MaxLength="23"
EnableSingleInputRendering="False"
CssClass="signuptxt">
<EnabledStyle Width="250px" />
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server"
ControlToValidate="txtFirstName"
ErrorMessage="First Name is required"
ToolTip="First Name is required"
ForeColor="Red"
ValidationGroup="regcust">*
</asp:RequiredFieldValidator>
<telerik:RadButton ID="btnRegister" class="submit" type="submit" runat="server" Text="Register Customer" Width="210px" Font-Size="Large"
Skin="buttonRed" EnableEmbeddedSkins="false" ValidationGroup="regcust" OnClick="btnRegister_Click">
</telerik:RadButton>
Use Page.IsValid in your server side code to verify your client validation is working also try using regular expression validator for blank spaces. It would be better if you could add an empty entry check in your stored procedure.
Try adding this causesvalidation="true" to your button .
<telerik:RadButton ID="btnRegister" class="submit" type="submit" causesvalidation="true" runat="server" Text="Register Customer" Width="210px" Font-Size="Large"
Skin="buttonRed" EnableEmbeddedSkins="false" ValidationGroup="regcust" OnClick="btnRegister_Click">
</telerik:RadButton>
A submit button will always submits the form , It's default behavior ,so no matter what it will submit your form ,try using return false; on OnClick . It will stop form to do its default behavior .
I have zero knowledge about telerik so i am just suggestion on the basis of what i know hope it works for you . :-)
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on the client side when I press the Cancel button, which has the CausesValidation attribute set to "False". How can I get this to stop?
I do not believe that Validation Groups are supported in 1.1.
Here's a code sample:
<asp:TextBox id="UsernameTextBox" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="UsernameTextBoxRequiredfieldvalidator" ControlToValidate="UsernameTextBox"
runat="server" ErrorMessage="This field is required."></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="UsernameTextBoxRegExValidator" runat="server" ControlToValidate="UsernameTextBox"
Display="Dynamic" ErrorMessage="Please specify a valid username (6 to 32 alphanumeric characters)."
ValidationExpression="[0-9,a-z,A-Z, ]{6,32}"></asp:RegularExpressionValidator>
<asp:Button CssClass="btn" id="addUserButton" runat="server" Text="Add User"></asp:Button>
<asp:Button CssClass="btn" id="cancelButton" runat="server" Text="Cancel" CausesValidation="False"></asp:Button>
Update: There was some dynamic page generating going on in the code behind that must have been messing it up, because when I cleaned that up it started working.
Validation Groups were not added to ASP.NET until version 2.0. This is a 1.1 question.
Double check your setting and make sure you are not overwriting it in the code behind.
Are they in separate validation groups (the button and validator controls)?
You're not manually calling the JS to do the client validation are you?