can't get an asp:RegularExpressionValidator to work - c#

I'm having an issue getting a regex field validator to work for an asp page i'm trying to update.
Here is the asp:Panel stripped down to the important bits:
<asp:Panel ID="pnlEmailAddressCollection" runat="server">
<div id="POMInput-wrapper">
<div class="POMInput-FieldText">
<span class="POMInput-wrapper-text">Name:</span>
<br />
<span class="POMInput-wrapper-text">Email Address:</span>
<br />
</div>
<div class="POMInput-FieldEntry">
<asp:TextBox ID="txtEmailAddress" name="emailAddress" runat="server" CssClass="textInput"></asp:TextBox>
<asp:TextBox ID="txtUserName" runat="server" name="firstName" CssClass="textInput"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="rfvNameValidator" runat="server"
ErrorMessage="Please enter your name"
ControlToValidate="txtUserName"
Display="None" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please enter your email address"
ControlToValidate="txtEmailAddress"
Display="None" />
<asp:RegularExpressionValidator ID="rfvEmailValidator2" runat="server"
ErrorMessage="Please enter a valid email address"
ControlToValidate="txtEmailAddress"
Display="None"
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="true"
ShowSummary="false"
EnableClientScript="true" />
</div>
</asp:Panel>
It is currently failing on any email i put in. The asp:RequiredFieldValidator's work as expected.
I tested the regular expression in a test project and the regex seems good (returns true on valid emails, false on invalid ones). Did I set up the asp:RegularExpressionValidator incorrectly?

You should remove double backslash:
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
Note that you put two backslashes where you meant only one. If you were to set this expression from code behind, the string you provided is correct. But in aspx you don't have to escape backslash.
At the moment accepted email address would be something like abc#abc{backslash}.com

You can try with this code
ValidationExpression="[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
Nota : you can delete ^ and $ symbols

The regular expression works within .NET (server-side), but is failing due to the client-side JScript implemetation as documented in the Remarks section. To verify this (it passes server-side validation), set the EnableClientScript property on the validator to false.
Then undo that change and verify the regex will pass on the client side. You can use an online tester, if it's easier for you.

Related

Is there other way to hide the password in textbox (***) aside from textboxmode="password"?

It seems like if I use TextMode="password"it changes the size of the textbox and also the placeholder text style. It doesn't match the other textboxes that I made. Although it does the work for me, just curious if there is another way to make it happen to avoid ruining the design. Thanks!
<div class="form-group" style= "float:left">
<asp:TextBox ID="txtPW" runat="server" name="form-password-name" Width="200px"
placeholder="Password..." class="form-password form-control"
TextMode="Password"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server"
ErrorMessage="Only letters and numbers are allowed" Display="Dynamic" ControlToValidate="txtPW"
ForeColor="Red" ValidationExpression="^[a-zA-Z0-9]+$">
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="Password is required." ControlToValidate="txtPW" Display="Dynamic" ForeColor="Red">
</asp:RequiredFieldValidator>
</div>
you can use a regular html textbox:
<input type="password" name="form-password-name" id="txtPW" class="form-password form-control"/>

CompareValidator without RequiredFieldValidator?

So I've been looking all over and can't seem to find a similar problem.
Basically, it seems like using CompareValidator doesn't work without a RequiredFieldValidator.
<div class="control-group">
<label class="control-label" for="PositionName">
Password:</label>
<div class="controls">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<%--<asp:RequiredFieldValidator ID="rvPassword" runat="server" ControlToValidate="txtPassword"
ErrorMessage="Please Enter Password" SetFocusOnError="True" ValidationGroup="1"
CssClass="error"></asp:RequiredFieldValidator>--%>
</div>
</div>
<div class="control-group">
<label class="control-label" for="PositionName">
Confirm Password:</label>
<div class="controls">
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqConPass" runat="server" ControlToValidate="txtConfirmPassword"
ErrorMessage="Please Enter Confirm Password" SetFocusOnError="True" ValidationGroup="1"
CssClass="error"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="compPassword" runat="server" ControlToValidate="txtConfirmPassword"
ControlToCompare="txtPassword" ErrorMessage="Password Mismatch" SetFocusOnError="True"
ValidationGroup="1" CssClass="error"></asp:CompareValidator>
</div>
</div>
Basically, you can see I have the RequiredFieldValidator commented out for both pass and confirm pass. When I do this, I can submit with only a value in the txtPassword textbox and nothing in the txtConfirmPassword textbox.
If I uncomment the RequiredFieldValidators then it compares as it should.
If it helps, the reason I need to do this is because I am unable to decrypt the password and autofill the textbox with their current password. So whenever a user is editted, they will need to enter a new password everytime with a RequiredFieldValidator on it.
So my solution was to get rid of the RequiredFieldValidator and just check if the text is null or empty, and if it is, don't update the password, but if it isn't then update the user without updating the password.
I hope this makes sense, and if anyone can help I would greatly appreciate it.
If you need more info please ask.
Thanks again!
See this code snippet, in this first for password i have used Regular expression validator and once password is valid then i have enabled compare validator.
<script>
function Validate() {
if (document.getElementById('<%=txtPassword.ClientID %>').value != "")
ValidatorEnable(document.getElementById('<%=ConfirmPasswordRequired.ClientID %>'), true);
else
ValidatorEnable(document.getElementById('<%=ConfirmPasswordRequired.ClientID %>'), false);
}
</script>
<p>
Password
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RegularExpressionValidator ID="PasswordRegularExpression" runat="server"
ErrorMessage="*Password must be at least 8 characters long and include at least one Special Character, one Number, and one Capital letter."
ValidationGroup="ValidationGroup1" ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).*$"
ControlToValidate="txtPassword" >
</asp:RegularExpressionValidator>
</p>
<p>
Confirm Password:
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="txtConfirmPassword"
ErrorMessage="*Confirm Password is required."
Enabled="false" ValidationGroup="ValidationGroup1"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="NewPasswordCompare" runat="server" ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword" ErrorMessage="*Confirm Password must match with the Password."
ValidationGroup="ValidationGroup1"></asp:CompareValidator>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Save" ValidationGroup="ValidationGroup1"
OnClick="Button1_Click" OnClientClick="Validate();" />
</p>
Here's one thought, I also ended up using this solution:
How about setting the compare validator to validate the password textbox and compare it to the confirmation.
This way, the compare validator only fires if there is a value inside the password textbox.
<div class="control-group">
<label class="control-label" for="PositionName">Password:</label>
<div class="controls">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="PositionName">Confirm Password:</label>
<div class="controls">
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"/>
<asp:CompareValidator ID="compPassword" runat="server" ControlToValidate="txtPassword"
ControlToCompare="txtConfirmPassword" ErrorMessage="Password Mismatch" SetFocusOnError="True"
ValidationGroup="1" CssClass="error"/>
</div>
</div>

Required attribute in ASP Control

I want to know how to use required attribute in asp.net. .In html I used required attribute for mandatory fields. How to use it in asp .net?
I tried this so far, is it correct?
<p class="field-wrapper required-field">
<label>First Name</label>
<asp:TextBox ID="TextBox1" name="f_name" runat="server" required>
</asp:TextBox>
</p>
there's no required attribute. you have to use a RequiredFieldValidator
<asp:TextBox ID="TextBox1" name="f_name" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="TextBox1" errormessage="Please enter a value!" />

Error when using RequiredFieldValidator control for FreeTextBox control

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";
}

Validation control not firing on form submit

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.

Categories