I want to do validation in asp.net with javascript on age and mobile number. Can you suggest how I could do this?
Check out jQuery and the jQuery validation plugin which will handle these either directly or via the ability to extend the validation rules. That's the code that I would use.
For the age validation you can use a RangeValidator and for the mobile number you can use a RegularExpression validator.
For age you will want a validator that restricts the textbox to numbers only
For mobile number, you will want numbers only, and also "+" and maybe "-"
I don't want to write a tutorial on validator controls here, but you'll need a RangeValidator* for the age and a RegularExpressionValidator with two regexes and a ValidationSummary control for the error messages. Set EnableClientScript on the two validators to true.
There's a large amount of How Tos on the MSDN site.
* I suggested the wrong one there initially and copied George for RangeValidator
Related
I have a website in ASP.NET (WebForms, NOT MVC) which has a survey form divided in several slides. Each slide has a next button that, obviously does a transition (client-side, not post back or remote request) to the next slide.
In each slide I have several ASP.NET controls with their related validators. I want this validators to be triggered when I click the next button (or maybe when each input loses focus?).
I remembered ASP.NET doing client side validation on lost focus, but maybe I'm wrong... (I quit doing ASP.NET development about 3 years now, so I can't remember)
Thanks
UPDATE:
It would be better to make ASP.NET trigger each validator when the associated control lost focus. I remember ASP.NET doing this (or am I dreaming? =P)
First you need to make sure all of your validators have target controls specified using the "TargetControlID" Attribute on the validators.
Then you can set up a validation group per page and specify the group name in your next button and on the controls themselves.
If you are using regular expression validators you can choose them from this website
To Validate Client Side
If you are using custom validators you can create a client function and specify it on the custom validator using the ClientValidationFunction attribute and by setting EnableclientScript = "true" on the custom validator.
Just be sure your client function has sender and args parameters.
It looks like there's a supplied JavaScript function called Page_ClientValidate which should be callable to check the validation manually from JavaScript. I haven't used it, though, so YMMV.
put all your client-side validators into the same validationgroup and with your 'next' button add the same validation group. When you click the button it will automatically trigger all the validators before it does the post-back.
as to manually triggering the validation...
you might also be able to use ValidatorOnSubmit(). I remember doing this in another project but i'm having a hard time finding the code.
It seems that enabling 'SetFocusOnError' on each validator triggers the validation whenever I try to leave the field.
In short decorate your model, now Data Annotations are supported from Asp.Net 4.5
Check my Answer here..Client side webform validations
Hi
I need to validate two fields in an Asp.net form, where the requirements is like any one of them is required. For example, there is Page title and sub-heading input boxes, so any one of them is required. Can I do it using the validation controls Asp.Net provides?
Any kind of help is greatly appreciated. Thanks in advance.
You can use a CustomValidator control (MSDN) in ASP.NET for special situations that are not supported by any one of the other standard validators. It was created for this reason.
Microsoft describes how to create a custom validation function here.
Here's another tutorial on implementing it.
Or if you google for keywords like "creating a custom validator in asp.net" you can pick and choose from various solutions for your own project.
For your case, as an alternative to using a CustomValidator, you could explicitly change whether your required field validators are enabled by using the ValidatorEnable() JavaScript function.
// disable validation control
ValidatorEnable(RequiredFieldValidator1, false);
You can then write custom logic in JavaScript to determine the case in which each validation control is either enabled or disabled, and tie it to one of the (client-side) events of the text boxes (onblur, onchange, onkeyup, etc).
Then, on the server side you can write similar logic to do the same thing by setting the "Enabled" property and put this logic in your button click event before you check the IsValid state.
If all you are doing is conditionally determining when something is required, changing the enabled state is your best bet. Exactly what can be done is documented in ASP.NET Validation In Depth.
I have 4 textboxes which can only take a number. The problem is that I have to create 4 separate validators for the controls and associate their id with the validator. Is it possible to have just one asp.net regular expression validator and associate all the controls with that one validator?
You could use CustomValidator and write your own validation code. The clientside code could hypothetically access each text box and check them against the regex...
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
A simpler solution if you don't want to have to include the regex in 4 places is to store the regular expression in the code-behind and apply it to all the validators during Page Load? Not a perfect sol'n but would work. You could also apply a standard error message and other formatting options in the code-behind.
No, because each validator has a "controltovalidate" property that can only be set to a single value. The only exception is the CustomValidator
I would like to validate the textbox for specific text and it must not be blank. But the regular expression validator is not validating if the text box is BLANK. However, it validates if I type something in the text box.
How can I make regular expression to trigger even if the text box is empty?
Should I use Required Validator + Regex Validator at the same time? Thanks.
<asp:TextBox ID="txtcard" runat="server" MaxLength="16"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtcard" ErrorMessage="Please type credit card no"
ValidationExpression="^\d{16}$"></asp:RegularExpressionValidator>
You should combine your RegularExpressionValidator with a RequiredFieldValidator.
If either fails it will block due to validation firing. Each one serves a purpose and the RegularExpressionValidator's purpose is to validate entered text not the lack of text.
If you want to do it all in one validator your could use the CustomValidator and set ValidateEmptyText='true'. Then you could use the javascript regex to do the checking. I would recommend the two validators though as this is a standard approach.
I would generally do as you suggest and have a required validator as well. This would allow you to have a different message for each rule.
Another option that I would recommend any web developer look at is the JQuery validation plugin. If you combine this with Fluent Validation, you can keep all of your validation rules for your business objects in one place and you can validate on the Client side and at the Server using those same rules.
JQuery Validation
Fluent Validation
You should use both at the same time. Not returning a Validation error if the Value is blank is common with the ASP.NET validation controls. You will see the same behavior from the Validation attributes in the System.ComponentModel.DataAnnotations namespace.
Can you tell me when will use CustomValidator. For eg I have Textbox that accept comments from the user. Any reason why the CustomValidator would fit in scenario and what other validators cannot do or is difficult to do?
Your question describes the exact reason to use a CustomValidator: you use it when the existing validators won't do what you need done. For instance, there is no validator that will ensure that the value in text box B is between the values in A and C. You'd have to do that in code, in a CustomValidator.
CustomValidator is meant to give you freedom to implement your own validation logic where other validators just don't do what you need to do.
E.G: When you need to do any kind of validation that doesn't actually require a user input control (such as checking if a cookie exists) or if the control you want to validate has to follow input rules that are too involved for the other validators.
It depends what you are trying to validate - you will need to provide more information.
The CustomValidator lets you write any code you want to perform one or more validations on the data entered into the Textbox. The other validators perform a distinct function like comparing values, checking that a value has been entered or checking that a value conforms to a regular expression.
One thing I've used CustomValidators for in the past was generate a list of error messages to go in a ValidationSummary control. In general, use them whenever you would want to do some custom validation that the standard set of ASP.NET validators don't cover.