How to display messagebox for Custom Validator error message? - asp.net

In my user control i am having the code as:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:CustomValidator ID="cvOrgTypeGrid" runat="server" Enabled="false" Display="Dynamic"
ErrorMessage="Please add rules to get eligible members" >
</asp:CustomValidator>
In Code behind, I am having a function as:
public void Validate(bool isOrgType)
{
if (!isOrgType) return;
if (Rules.Any()) return;
cvOrgTypeGrid.Enabled = true;
cvOrgTypeGrid.IsValid = false;
}
I would like to get the error message "Please add rules to get eligible members" in messagebox.
Anybody please help out.

You can use a Validation Summary Control, check this msdn link

Related

CustomValidator doesnt display a message unless I use a validation summary

I want to output a message in case if an invalid date was supplied.
<asp:TextBox ID="RegistrationFromTextBox2" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="RegistrationFromTextBox2" OnServerValidate="CustomValidator1_ServerValidate" ValidationGroup="NewMailingItem" runat="server" ErrorMessage="The date is invalid"></asp:CustomValidator>
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
DateTime temp;
if (DateTime.TryParse(args.Value, out temp))
{
args.IsValid =true;
}
else
{
args.IsValid = false;
}
}
catch (Exception ex)
{
args.IsValid = false;
}
}
I expect the output message to be located near the field.
Instead I get no response, even though the function works. I only get the error message if I put a validation summary to my form.
Is there away display the message without the validation summary?
Is there away display the message without the validation summary?
You will need to use Text attribute instead of ErrorMessage.
<asp:CustomValidator ID="CustomValidator1"
ControlToValidate="RegistrationFromTextBox2"
OnServerValidate="CustomValidator1_ServerValidate"
ValidationGroup="NewMailingItem"
runat="server"
Text="The date is invalid">
</asp:CustomValidator>
FYI: If you just want to validate date, you could use CompareValidator or Ajax Control Toolkit's Calendar control.
<asp:TextBox ID="RegistrationFromTextBox2" runat="server"
placeholder="MM/DD/YYYY">
</asp:TextBox>
<asp:CompareValidator
ID="RegistrationFromCompareValidator" runat="server"
Type="Date"
Operator="DataTypeCheck"
ControlToValidate="RegistrationFromTextBox2"
Text="The date is invalid">
</asp:CompareValidator>

DotNetNuke CustomValidator strange behavior

I have a strange problem with a dotnetnuke module I'm developing.
I want to use a asp custom validator to validate some input. To keep it simple I'll check only if the field was not empty and at least a few characters long. (I know there are other standard validators that I can use).
The problem is that my code works ok locally (development), but not on production.
The only difference I know is that I use DNN 6 instead of DNN 5.
No matter what I type in on production site, it always shows me the validation error message.
These are the relevant parts of the webpage:
ASCX:
<div>
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
ErrorMessage="Error the field ... was not correct..." OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
<asp:Button ID="btnZoeken" CssClass="btnZoeken" CausesValidation="true" runat="server" Text="<%$ Resources:GLOBAL, btnZoeken %>"
OnClick="btnZoeken_Click" />
Code behind
private bool ValidateTab_Ondernemingsnummer()
{
if (!String.IsNullOrEmpty(txtOndernemingsnummer.Text) && txtOndernemingsnummer.Text.Length >= 3)
{
return true;
}
return false;
}
protected void CustomValidatorActiveTab_ServerValidate(object source, ServerValidateEventArgs e)
{
int activeTab = GetActiveIndexAccordion();
switch (activeTab)
{
//Zoeken op ondernemingsnummer
case 0:
if (!ValidateTab_Ondernemingsnummer())
{
e.IsValid = false;
}
else
{
e.IsValid = true;
}
break;
}
Thanks for any help.
Any ideas?
SOLVED:
I used dotnetnuke logging to see when and why e.isValid is set to false.
My custom validator control was being called twice!!
The first time it was validated ok, the second time it was not.
My solution was to disable custom server validator control in the markup and enable it just after doing the submit (and don't forget to turn it off).
Like this:
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False"
UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
EnableClientScript="false" Enabled="false" ErrorMessage="ERROR ONDERNEMINGSNUMMER"
OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
Enabled = false is important here!
Then in the button click
protected void btnZoeken_Click(object sender, EventArgs e)
{
CustomValidatorActiveTab.Enabled = true;
CustomValidatorActiveTab.Validate();
if (Page.IsValid)
{
CustomValidatorActiveTab.Enabled = false;
I still don't know why the CustomValidatorActiveTab_ServerValidate was being called twice.
It has something to do with DNN 5 I suppose (and maybe it was fixed in DNN 6).
I hope this helped someone.
The reason the validator is called twice is due to the link button.
Generating the GridView onBubbleEvent, which also causes the validator to validate.

how to make a validation code for ASP.NET form with VB coding

im trying to build a form+attachment that needs to be send to email.
Im using a VB background code (attachementemail.aspx.vb)
and my front (b-16.aspx)
I want the page to check that the user entered a email, name, phonenumber and attachment.
what command do I put in the axp.vb
and what on the .aspx
tried just about anything.
The simplest way is to use validators eg RequiredFieldValidator for mandatory fields. You can also implement CustomValidators for custom logic.
See http://msdn.microsoft.com/en-us/e5a8xz39.aspx for the available validators
At it's basic level you could use RequiredFieldValidator and CustomValidation in your form. You can use some regex logic for email, I use this but there are many out there:
Regex(#"\w+([-+.]\w+)#\w+([-.]\w+).\w+([-.]\w+)*")
Personally I use client side javascript before it hits the server and then I re-validate the entries once it hits the server. If your using the postback events then you'll need update panels and a scriptmanager (not sure if you are aware of this already, so apologies if teaching you to suck eggs!).
Here is an example:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Code behind (sorry this is in c#)
protected void Button1_Click(object sender, EventArgs e)
{
if (RequiredFieldValidator1.IsValid)
{
Label1.Text = "Has content";
}
else
{
Label1.Text = "Not valid";
}
}
Note that the required field validator has it's own methods to display a "hey you haven't entered content here my friend" message, but i have added that to the label instead.

No error message displayed for custom validator

I have a requirement that one of multiple fields is required. Using custom validator the even fires, false is returned, but no error message is display and the form validates.
What am I missing? I have tried with and without ValidationSummary.
Thanks!
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="validatePhone" EnableClientScript="false" runat="server" ErrorMessage="Home or Cell Phone is Required" ></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" DisplayMode="BulletList" runat="server" ForeColor="Red" Font-Size="X-Small" Font-Bold="true" />
protected void validatePhone(object sender, ServerValidateEventArgs e)
{
e.IsValid = string.IsNullOrEmpty(txtCellPhone.Text) && string.IsNullOrEmpty(txtHomePhone.Text) ? false : true;
}
You have to set the ControlToValidate to some TextBox.
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="validatePhone" EnabEnableClientScript="false" runat="server" ErrorMessage="Home or Cell Phone is Required" ControlToValidate="txtHomePhone"/>
Check out this article. Basically you need to wire up the client side validation.
Add the following just before the closing form tag changing the control names as needed:
<%-- This configures the validator to automatically--%>
<%-- update when either of these controls is changed --%>
<script type="text/javascript">
<!--
ValidatorHookupControlID("<%= MyControl1.ClientID %>",
document.getElementById["<%= CustomValidator1.ClientID %>"]);
ValidatorHookupControlID("<%= MyControl2.ClientID %>",
document.getElementById["<%= CustomValidator1.ClientID %>"]);
//-->
</script>
Alternatively use this control
Issue was completely my fault. On my submit button the final thing I do is a Response.Redirect. The message was coming up, but then the Thank you page was being presented. Now only doing the Response.Redirect if the customvalidator returns true.

Need help in login validation using javascript

Please could somebody point me out what is the error here? Using javascript i am validating the user input. When i click the login button without checking the javascript function, it goes to the welcome page. Here is my code:
<script type="text/javascript">
function validLogin()
{
if(document.getElementById("txtUserName").value == "")
{
alert("Please enter your UserName");
return false;
}
if(document.getElementById("txtPassword").value == "")
{
alert("Please enter your Password");
return false;
}
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
BtnLogin.Attributes.Add("onClick","return ValidLogin();");
}
I see that you're using ASP .NET (the Page_Load event on your posted code).
I think that will be easier to handle validation through ASP .NET Validation Controls, i.e. RequiredFieldValidator.
Check your case on return ValidLogin(); it doesn't match.
P.S.: I hope you aren't performing all user validation client-side.
It would probably be easier to user ASP .NET validation controls, here's a sample:
User Name:
<asp:TextBox ID="UserName" runat="server" />
<asp:RequiredFieldValidator
ID="UserNameValidator"
ControlToValidate="UserName"
ErrorMessage="User Name Required"
runat="server" />
Password:
<asp:TextBox ID="Password" runat="server" />
<asp:RequiredFieldValidator
ID="PasswordValidator"
ControlToValidate="Password"
ErrorMessage="Password Required"
runat="server" />
first make sure it's not a difference in case... your javascript function is validLogin and in Page_Load you have ValidLogin

Resources