I am trying to create a customvalidator to check if an email is already in the database at the point of an user registration. I have seen many similar questions dealing with customvalidators but no answer has brought any avail.
I have simplified the OnServerValidate method to always return false to get ANY sort of response but I am not getting any response. I believe that the method is never being fired. I have come to some conclusion there is some other issue that is effecting the triggering of the event.
Here is the ASP which is inside a contentplaceholder within the master page. There is also a scriptmanager in the master file.
<%--email and confirm email--%>
<td>
<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkEmail" runat="server" TargetControlID="Email" WatermarkText="Email" WatermarkCssClass="watermarked roundedcorner" />
<asp:TextBox ID="Email" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="UserInformation" CausesValidation="True"><font color="red">*</font></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red" ControlToValidate="Email" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
<asp:CustomValidator ID="CheckEmail" ForeColor="Red" ErrorMessage="This email already is registered." ValidateEmptyString="true" display="Dynamic" ControlToValidate="Email" runat="server" ValidationGroup="UserInformation" OnServerValidate="checkEmailValidator_ServerValidate"></asp:CustomValidator>
</td>
</tr>
<tr>
<td>
<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkConfirmEmail" runat="server" TargetControlID="ConfirmEmail" WatermarkText="Confirm Email" WatermarkCssClass="watermarked roundedcorner" />
<asp:TextBox ID="ConfirmEmail" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ConfirmEmail" ErrorMessage="Confirm E-mail is required." ToolTip="Confirm E-mail is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="red" ControlToValidate="ConfirmEmail" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
</td>
</tr>
And then the server-side script. This has a breakpoint at every line and NONE of them are ever getting hit. So I am assuming that the method is never getting triggered.....
protected void checkEmailValidator_ServerValidate(object sender, ServerValidateEventArgs args)
{
args.IsValid = false;
}
And here is the button to move to the next part of the form. It merely just hides one div and shows another to continue the registration process.
<asp:ImageButton ID="NextButton" runat="server" ImageUrl="images/registration/arrowright.png"
ClientIDMode="Static" AutoPostBack="False" OnClientClick="return false;"
Height="50px" onmouseover="this.src='images/registration/arrowrightgreen.png'"
onmouseout="this.src='images/registration/arrowright.png'" ValidationGroup="UserInformation" UseSubmitBehavior="False"/>
This button is used to move to a second part of the registration process, which I use jQuery for. This is why the OnClientClick="return false;" line is there so the jQuery will run correctly. Is there anywhere around this?
$("[id$=NextButton]").click(function (e) {
//alert(Page_ClientValidate("UserInformation"));
if (Page_ClientValidate("UserInformation")) {
$("[id$=pick_user_type]").css('position', 'absolute');
$("[id$=registration_div]").css('position', 'absolute');
$("[id$=registration_div]").hide('slide', { direction: 'left' }, 1000);
$("[id$=pick_user_type]").show('slide', { direction: 'right' }, 1000);
}
});
When you define the validator group of a validator than you to also define the validation group of Controls which the validator is validating.
So define your textbox like this.
<asp:TextBox ID="Email" runat="server" Columns="48" CssClass="roundedcorner"
ValidationGroup="UserInformation"
></asp:TextBox>
If you didnt define the validation group of your button than define there validation group too/
Edit
OnClientClick="return false;" remove this line to make your button postback. If you dont remoove than your form never submited means it will never send request on server.
Related
So, basically I have multiple TextBoxes on my page and some of them have validations with RequiredFieldValidator.
I am using OnTextChanged event on txtMobileNo TextBox Field to fetch the data from database.
If the number exists in the database the the rest of my TextBoxes filled from server side.
Now, I am facing here a problem: my MobileNo Text Field skips the RegularExpressionValidator validation even if it is wrong.
I want to redirect to server side only if my number matches the validation logic.
<div class="col-md-4">
<asp:Label ID="lblfortxtMobileNo" runat="server" Text="Mobile No"></asp:Label>
<asp:TextBox ID="txtMobileNo" runat="server" AutoPostBack="true" OnTextChanged="txtMobileNo_TextChanged" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="rqfvtxtMobileNo" runat="server" ErrorMessage="*" ControlToValidate="txtMobileNo" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Please Enter Valid Number"
ControlToValidate="txtMobileNo"
Display="Dynamic"
ForeColor="Red"
ValidationExpression="^([0-9]{10})$"
></asp:RegularExpressionValidator>
</div>
<div class="col-md-4">
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtName" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
</div>
<div class="col-md-4">
<asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" />
</div>
When I am set CauseValidation = "true" the result is somewhat what I want .
But it checks all the other RequiredFieldValidator validation as well before calling OnTextChanged function.
I just want to check Regular Expression Validation at this point for txtMobileNo and based on the result wants to display error.
Cross check your regex id it is look like an duplicated id here.
I've got two text boxes like this:
<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." />
It's two text boxes that have regular expression validators that constrain them to only allow positive real numbers. These work fine.
I also want the input in the second text box to be smaller than the first one. For that I have a compare validator.
When the user has correct numbers the compare validator works fine.
It's when they enter in anything that fails the second regular expression validator, that the compare validator also fires at the same time.
It doesn't matter what's in the first text box, valid input, wrong input, or even nothing. Both of the second validators fail.
Even though the validator is supposed to be comparing doubles.
Is there an easy fix for this?
I realize that this behavior is okay, because a validator is invalid when it should be, but the user would be seeing the incorrect error messages.
I've already done a solution involving custom validators and Javascript, and if it comes down to it then I'll have to do that again. But if that's the case then there's not much point to using a compare validator, since it'll never work with a regular expression validator.
Assign different validation groups to regular expression validator and compare validator and use ASP.NET inbuilt javascript function Page_ClientValidate() to check validation one by one.
<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2"/>
<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." ValidationGroup="Group3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />
In Javascript
<script type="text/javascript">
function Validate() {
var isValid = false;
isValid = Page_ClientValidate('Group1');
if (isValid) {
isValid = Page_ClientValidate('Group2');
}
if (isValid) {
isValid = Page_ClientValidate('Group3');
}
return isValid;
}
</script>
I think you should use Custom Validator for second textbox to confirm that Comparison logic occurs if regular expression validation passes. You could do it the following way-
<asp:Textbox id="textBox2" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2" runat="server"
ControlToValidate = "textBox2"
ErrorMessage = "Your Error Message"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
<script type="text/javascript">
function validateLength(oSrc, args){
// your validation logic here
}
</script>
I'm having issues with asp.net's field validators taking up space on my page. I've searched around, and documentation says to use Display="Dynamic" to keep the validators from taking up space. When I use this however, the error messages are always displayed.
What am I doing wrong?
I just want the error messages to display when the user either clicks the "Save" button, or loses focus on a textbox. And I don't want the validators to take up space.
<p>Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGgroup="Email"
ErrorMessage="Please enter an email"
ControlToValidate="TextBoxEmail" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
ValidationGroup="Email"
ControlToValidate="TextBoxEmail"
ErrorMessage="Please enter valid email"
runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" />
<p>Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:LinkButton ValidationGroup="Email" runat="server" Text="Save" OnClick="linkbuttonSave_Click" />
Only thing I notice in your code is you have a typo in ValidationGgroup="Email". It should be ValidationGroup="Email"
Other than that, Display="Dynamic" works for me.
After clicking, submit button -
<p>
Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="Email"
ErrorMessage="Please enter an email" ControlToValidate="TextBoxEmail" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Email"
ControlToValidate="TextBoxEmail" ErrorMessage="Please enter valid email" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" />
<p>
Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="TextBoxEmail"
ControlToValidate="TextBoxEmail2" CssClass="failureNotification" Display="Dynamic"
ErrorMessage="Must match." ValidationGroup="Email"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="Email"
ErrorMessage="Please enter an confirm email" ControlToValidate="TextBoxEmail2" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:LinkButton ID="LinkButton1" ValidationGroup="Email" runat="server" Text="Save"
OnClick="linkbuttonSave_Click" />
You can set the property Display to "Dynamic" or "none", the second one will cause the Error message to be displayed only in the validation summary.
I got it to work great by deleting the "ValidationGroup=.." . Havent looked what up what this does but it works great without it and not sure why it would be needed (given the validator controls have the control id of the email textbox)
I have a piece of code which i need to fix.
<td>
<asp:TextBox ID="txtEmail" runat="server" CssClass="TextBoxCss"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqEmail" runat="server" ControlToValidate="txtEmail"
ErrorMessage="Enter Email" ValidationGroup="entryvalid" Display="None">
</asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender4" runat="server"
TargetControlID="reqEmail" CssClass="CustomValidator" />
<asp:CustomValidator ID="valEmail" ControlToValidate="txtEmail" Display="None"
runat="server" ValidationGroup="entryvalid" ErrorMessage="Invalid Email Address"
ClientValidationFunction="ValidateEmail" />
<asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server"
TargetControlID="valEmail" CssClass="CustomValidator" />
</td>
If i am leaving email box empty then it says "enter mail" and page.IsValid is returning False using the below code:
public bool IsValid
{
get
{
EnableValidation(true);
Page.Validate("entryvalid");
return Page.IsValid;
}
}
EmailValidation func :
function ValidateEmail(source, arguments) {
var val1 = $.trim(arguments.Value.toLowerCase());
var regexemail = /^([\w-_]+\.)*[\w-_]+#([\w-_]+\.)*[\w-_]+\.[\w-_]+$/;
arguments.IsValid = regexemail.test(val1);
}
But when i give invalid email ("user#gmail#.com") it says "Invalid Entry" but Page.IsValid is returning True.
Can anyone please mention where might be wrong?
I use this for email validation:
<asp:RegularExpressionValidator ID="valEmail" runat="server"
ErrorMessage="* Please enter a valid email address"
ControlToValidate="txtEmail"
ValidationGroup="Group1"
ValidationExpression="^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$">
</asp:RegularExpressionValidator>
This is client side validation rather than server side validation
EDIT
Currently the server does not know if the custom validator is valid or not.
You need to add: OnServerValidate="ServerValidation" to the custom validator in the aspx.
Then add the following server side:
void ServerValidation (object source, ServerValidateEventArgs arguments)
{
//perform your check here.
arguments.IsValid = false;
}
Now when Page.IsValid is called, the server will know about the custom validator.
See this article
Try this Aspx code:
<asp:RequiredFieldValidator runat="server" ID="rfvEmail" Display="None" ControlToValidate="txtEmail"
InitialValue="" ValidationGroup="SubscriptionDetails" ErrorMessage="Email is Mandatory"
ForeColor="Red"></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vceEmail" TargetControlID="rfvEmail" runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]#[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
ControlToValidate="txtEmail" ErrorMessage="Invalid Email" ValidationGroup="SubscriptionDetails"
Display="None"></asp:RegularExpressionValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vce1Email" TargetControlID="regexEmailValid"
runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged"
Width="90px" AutoPostBack="true" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="Textboxtotalamount"
SetFocusOnError="True" ValidationGroup="val">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="Textboxtotalamount"
ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$"
SetFocusOnError="True">F</asp:RegularExpressionValidator>
in this above code can validate correctly.......but in text box i call "OnTextChanged"
event ....suppose if i type(characters)in textbox errormesge shown at the same time ontextchanged event also called hen error occured ....i ve to stop OnTextChanged event when regularexpression raised......
pls help
I updated your code. see if this helps you.
<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged" ValidationGroup="val"
Width="90px" AutoPostBack="true" runat="server" CausesValidation="true">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ValidationGroup="val"
ControlToValidate="Textboxtotalamount"
SetFocusOnError="True" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="Textboxtotalamount" ValidationGroup="val"
ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$"
SetFocusOnError="True">F</asp:RegularExpressionValidator>
I'm not sure I understood your question correctly: You want to execute the `AmountChanged´ code only if the validator did not report any errors, right? In that case, you should call
if (!this.IsValid)
return;
at the start of AmountChanged. (You might need to call this.Validate(); first, if OnTextChanged does not initiate validation by itself.)
Details can be found here: http://msdn.microsoft.com/en-us/library/dh9ad08f(VS.100).aspx
Clarification: At the moment, your code looks like this:
void AmountChanged(...) {
...
}
You need to change it to this:
void AmountChanged(...) {
if (!this.IsValid)
return;
...
}
so that the code is not executed when some validator detects an error. In addition, you must add CausesValidation="true" to your TextBox as Saar's example shows.