validation step by step - asp.net

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.

Related

Differential validation of a textbox in asp.net webforms

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.

How to display save confirmation message on my Web form when user saves new form by clicking Save button

I am very new to ASP.NET. I am working on some other developer's Web form. I have been asked to fix a bug which says:
No success message is displayed when user clicks Save button after
adding information to the form (first time or while updating)
There is a Label at the top of the page with ID lblMsgs
<asp:Label ID="lblMsgs" runat="server">
I have added this to the Code behind in the appropriate place:
lblMsgs.Text = "Message Text";
Now, my question is how do I modify the HTML or code behind to make sure that when Save button is clicked, the code checks that the data is saved to the database and displays save successful message on the Web form.
Can you please help by giving example of the code using my label ID above?
Thank you in advance.
That's a pretty vague question, but if your looking for data validation you should look at the built in ASP.NET Validators. This can do client/server side data validation. As for returning a message to the user based on a successful update to the DB, that is going to be solution specific depending on what type of data layer your using.
On your Save button handler, you can add validation logic if whether the data is valid or not.
protected void ValidateSave(object o, EventArgs e){
if (Page.IsValid) {
// Save to DB and notify of update status
var success = SomeDBMethod.Update...
if (success){
DoAlert("Saved Successfully!");
}
}
else { lblMsgs.Text = "Failed"; }
}
// Call client-side alert
private void DoAlert(string message) {
ClientScriptManager cs = Page.ClientScript;
var x = "alertScript";
if (!cs.IsStartupScriptRegistered(cstype, x))
{
String script = string.Format"alert('{0}', message);";
cs.RegisterStartupScript(cstype, x, script, true);
}
}

asp:CustomValidator not returning 'false'

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.

Conditional Validation ASP.NET

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.

Custom Validator with an OR Condition

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

Resources