Right now i have an asp.net 2.0 app which allows a user to search by the following fields
Location (Required if there is nothing in idnumber field)
Address (Required if there is nothing in idnumber field)
Zip (Required if there is nothing in idnumber field)
**OR**
IDNumber. (Required if there is nothing in any of the other fields)
What i'd like to be able to do is validate this client side on button click and display a summary of errors.
i.e.
if a user leaves every criteria blank. I'd like to display "You must enter a IDNumber or "Location, Address, and Zip to continue"
I've never used the Custom Validation control so here are some questions.
1) Is it able to do this?
2) Does anyone have an example of how to do this?
Thanks
You can use the ClientValidationFunction property of a CustomValidator control to specify a Javascript function that will validate your form. You'll need to write the JavaScript for the validation. Unless you're writing an application where you can be absolutely sure that all of your clients have JavaScript enabled, I highly recommend you also use the OnServerValidate property to also provide server-side validation.
Server-side CustomValidator example: https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx
Client-side example: http://www.codeproject.com/KB/custom-controls/CustomValidatorAndSummary.aspx
It is fairly simple to use a custom validator. Add one to your page, and choose the ServerValidate event, which will generate a function like this (example in C#):
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
// Your validation logic goes here... set args.IsValid = true if it passes,
// false otherwise. Here is an example...
args.IsValid = false;
if(txtIDNumber.Text.Length > 0)
{
args.IsValid = true;
}
else if (txtLocation.Text.Length > 0
&& txtAddress.Text.Length > 0
&& txtZip.Text.Length > 0)
{
args.IsValid = true;
}
}
Related
I have an aspx page with three textbox controls, and i want to do validation for all.
i want to show the error message in a sequence,
for example if i left all three text boxes empty and click on submit button,
first it will show error message for first text box and then when i fill the first textbox and click again save then it shows the error message for the second textbox and so on.
that means i only want to show one error message at a time after clicking the submit button.
can you people suggest me the way.
You could do it though checking on submission of the form and posting back to a literal with error messages. Postback validation is better than just relying on javascript in any case.
So, depending on the level of checks you want to employ, you could do something like:
protected void btnSubmit_Click(object sender, eventArgs e)
{
if(String.IsNullOrEmpty(txtBox1.Text))
{
ltError.Text = "Sorry, error message for box1";
return;
}
}
Obviously you'd work in more checks and after passing stage1 you'd move on to stage 2. Not great from a user experience but would work.
you can return a function value like this
public string CheckValidation()
{
if (txtFirstName.Text.Trim().Equals(string.Empty))
return "Please enter firstname";
if (txtLastName.Text.Trim().Equals(string.Empty))
return "Please enter lastname";
}
and so on as per your validation fields.
Hope this helps you
Can you use the AJAX control toolkit? The VaidatorCallout controls behave this way and you get a nice little balloon indicating where the error is.
ASP.NET Validator Callout
I'd recommend an <asp:CustomValidator> control for each textbox. You can use something like the following for the custom validation routines:
var textBox1IsValid = function textBox1IsValid(sender, args) {
var tb = document.getElementById('TextBox1'),
resultOfValidation = false;
//do validation, modifying resultOfValidation as needed.
arg.IsValid = resultOfValidation;
return resultOfValidation;
},
textBox2IsValid = function textBox2IsValid(sender, args) {
var tb = document.getElementById('TextBox2'),
resultOfValidation = false;
//do validation, modifying resultOfValidation as needed.
//return either the validity of TextBox2
//or (if TextBox1 is not valid) return true so the
//validator for TextBox2 doesn't fire.
arg.IsValid = resultOfValidation || !textBox1IsValid(sender, args);
return resultOfValidation;
},
textBox3IsValid = function textBox3IsValid(sender, args) {
var tb = document.getElementById('TextBox3'),
resultOfValidation = false;
//do validation, modifying resultOfValidation as needed.
//return either the validity of TextBox3
//or (if either TextBox1 or TextBox2 is not valid) return
//true so the validator for TextBox3 doesn't fire.
arg.IsValid = resultOfValidation || !textBox1IsValid(sender, args) || !textBox2IsValid(sender, args);
return resultOfValidation;
};
The advantage of this approach is that TextBox2 and TextBox3 will return as valid if their contents are valid or if TextBox1 is not valid. This will fire only one validator at a time, until all fields are valid. It's also a little more flexible as your custom validation routine can check for:
required field
pattern matching
value comparison
or any other validation that you need, all wrapped up into one function.
The downside is that you'll also need to duplicate the validation logic server-side.
I am working on an asp.net webforms project. I have a usercontrol with a boolean public property, named as IsRequired, which can be set either to true or false. I have a text box where the user will enter the social security number (SSN). If IsRequired = true, then the user has to enter the SSN value, otherwise validation should fail and "SSN is required" message should be displayed next to the textbox. If IsRequired = false, then if the user doesn't enter a SSN value, then a messagebox should be displayed with the message, "If you have a SSN, please enter it". Essentially the message encourages the suer to enter SSN. If the click "OK" on the messagebox, then they could enter the value. otherwise they could submit the page, and the validation should not fail.
I am using DevExpress controls, and came up with the following solution:
on ascx page:
<dx:ASPxTextBox ID="SsnTextBox" runat="server" OnValidation="SocialSecurity_Validation">
<MaskSettings Mask="999999999"></MaskSettings>
</dx:ASPxTextBox>
on the codebehind:
if (IsSubscriber && (e.Value == null || (string)e.Value == ""))
{
SsnTextBox.ErrorText = "Required";
e.IsValid = false;
}
else if (!IsSubscriber && (e.Value == null || (string)e.Value == ""))
{
e.IsValid = true;
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "UserControl",
"confirm('If Social Security Number is available, please enter it!')", true);
}
The problem with the code it works only partially. For example, the message box is displayed, but when either ok or cancle is clicked, the page is posted without giving the option to enter a value. If IsRequired = true, and a value is not entered, validation fails, still the page gets submitted. Any help to resolve this or a better code is appreciated. The solution need not be DevExpress specific, instead it could a generic one.
Thanks
Rather than use the confirm method; which will return either a true or false depending on what the user selects, why not use Alert() instead?
I.E
return Alert('If Social Security Number is available, please enter it!')
There won't be a choice for the user - just an OK button. 'return' will also prevent the postback.
Don't know what is wrong here. This is a page developed by someone else and I am trying to fix one of the issue.
Scenario:
ASP.NET site.
Login.aspx has <asp:login> and there are three validation groups.
Login.aspx.cs is a partial class of "user_login".
Each validation group has a textbox and an associate customvalidator. All three custom validators gets triggered when something is entered in corresponding textbox but issue is only the first textbox (bound to validationgroup = 1) returns false when validation fails.
For 2nd and 3rd, the customvalidator get triggered but when there is validation issue and even after setting "args.IsValid = false;", the process continues with what needs to be executed further.
Don't know what is going on wrong here. I would like the customvalidator to return false. Worst case, are there any ways to return the control back to the 'textbox' when validation fails?
Below is the custom validator used.
<asp:CustomValidator ID="ExistingIdValidator" runat="server" ControlToValidate="NewUserName"
ValidateEmptyText="true" ValidationGroup="NewUserForm"
OnServerValidate="NewUserNameCheck_ServerValidate">
</asp:CustomValidator>
protected void NewUserNameCheck_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator validator = source as CustomValidator;
if (validator != null)
{
string NewuserNameValue = this.NewUserName.Text;
Guid registeredUserId = (Guid)Membership.GetUser(NewuserNameValue).ProviderUserKey;
if (registeredUserId != null)
{
validator.IsValid = false;
this.FailureText_New.Text = "This UserID already exists. Please login as existing user";
args.IsValid = false;
}
}
}
The funny thing about ASP.NET server-side validation is that it does not automatically prevent your click events from executing. It's as if the validation procedure is performed and then the results are ignored. So, the first thing to put inside your button's event is if (!Page.IsValid) return;. That's how you prevent the rest of the event from executing, and that's how you force the user to correct any mistakes on the form.
I am trying to use the jQuery UI autocomplete feature in my web application. What I have set up is a page called SearchPreload.aspx. This page checks for a value (term) to come in along with another parameter. The page validates the values that are incoming, and then it pulls some data from the database and prints out a javascript array (ex: ["item1","item2"]) on the page. Code:
protected void Page_Load(object sender, EventArgs e)
{
string curVal;
string type ="";
if (Request.QueryString["term"] != null)
{
curVal = Request.QueryString["term"].ToString();
curVal = curVal.ToLower();
if (Request.QueryString["Type"] != null)
type = Request.QueryString["Type"].ToString();
SwitchType(type,curVal);
}
}
public string PreLoadStrings(List<string> PreLoadValues, string curVal)
{
StringBuilder sb = new StringBuilder();
if (PreLoadValues.Any())
{
sb.Append("[\"");
foreach (string str in PreLoadValues)
{
if (!string.IsNullOrEmpty(str))
{
if (str.ToLower().Contains(curVal))
sb.Append(str).Append("\",\"");
}
}
sb.Append("\"];");
Response.Write(sb.ToString());
return sb.ToString();
}
}
The db part is working fine and printing out the correct data on the screen of the page if I navigate to it via browser.
The jQuery ui autocomplete is written as follows:
$(".searchBox").autocomplete({
source: "SearchPreload.aspx?Type=rbChoice",
minLength: 1
});
Now if my understanding is correct, every time I type in the search box, it should act as a keypress and fire my source to limit the data correct? When I through a debug statement in SearchPreload.aspx code behind, it appears that the page is not being hit at all.
If I wrap the autocomplete function in a .keypress function, then I get into the search preload page but still I do not get any results. I just want to show the results under the search box just like the default functionality example on the jQuery website. What am I doing wrong?
autocomplete will NOT display suggestions if the JSON returned by the server is invalid. So copy the following URL (or the returned JSON data) and paste it on JSONLint. See if your JSON is valid.
http://yourwebsite.com/path/to/Searchpreload.aspx?Type=rbChoice&term=Something
PS: I do not see that you're calling the PreLoadStrings function. I hope this is normal.
A couple of things to check.
Make sure that the path to the page is correct. If you are at http://mysite.com/subfolder/PageWithAutoComplete.aspx, and your searchpreload.aspx page is in another directory such as http://mysite.com/anotherFolder/searchpreload.aspx the url that you are using as the source would be incorrect, it would need to be
source: "/anotherFolder/Searchpreload.aspx?Type=rbChoice"
One other thing that you could try is to make the method that you are calling a page method on the searchpreload.aspx page. Typically when working with javascript, I try to use page methods to handle ajax reqeusts and send back it's data. More on page methods can be found here: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
HTH.
I have 2 TextBoxes (textBoxA, textBoxB), both watched by their own RequiredFieldValidator. I want to 'enable' the RequiredFieldValidator for textBoxB just when textBoxA has a value (or meets some specific conditions).
Use cases:
Case 1
textBoxA = ""; -> Show Required Field Validation Message
textBoxB = ""; -> Do not show validation message
Case 2
textBoxA = "has a value";
textBoxB = ""; -> Show Required Field Validation Message
Case 3
textBoxA = "has a value";
textBoxB = "has a value too";
Thanks for your help!!
You might want to use a CustomValidator to do this. You'll need to implement the client side and server side validation. Something like (off the top of my head and untested)
Server side
protected void ServerValidation (object source, ServerValidateEventArgs args)
{
if (!string.IsNullOrEmpty(textBoxA))
args.IsValid = !string.IsNullOrEmpty(textBoxB);
}
Client Side
function clientValidation(sender, args) {
if (args.value !== "") {
var textBoxB= document.getElementById('textBoxB');
args.IsValid = (textBoxB.value !== "");
}
return;
}
In this situation I'd use a CustomValidator for textBoxB instead of the required field validator. In the server side validation method you can control the exact nature of the validation with something like this.
if (textBoxA.Text != string.Empty)
{
args.IsValid = textBoxB.Text != string.Empty;
}
I don't believe there's a declarative way to do it. I've always done this by having a ValidatePage method where I set my validators to enabled or disabled and then call Page.Validate at the end (and then proceed or render based on Page.IsValid).
So, either
validator2.IsEnabled = textBoxA.Text.Trim().Length > 0
or something like that.
That's pseudo code btw...I haven't done ASP.NET in some time now.
What I do is to change the Validation Group according to requirement, for example, you may assign the validation group to the textBoxB to a value different or the same that textBoxA.ValidationGroup & the submit control, this may be done in textBoxB's Onchange.
Validators which are evaluated are all that correspond to the same validation group of the submit control.