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.
Related
Background: I have a toolset of CompositeControls that I use to build forms. The controls themselves tie in to some jquery validation for basic validation tasks.
Problem: I want to make the validation of these CompositeControls more flexible by allowing developers to use the ASP.NET validators (i.e. RegExValidator, RequiredFieldValidator, CustomValidator, etc) to validate the data in one of my CompositeControls.
Example: I'd like the developers to be able to do something like this:
<asp:ValidationSummary runat=server HeaderText="There were errors on the page:" />
<custom:TextBox id='SomeTextBox' Label='Enter Name Here:' text='' runat='server' />
<asp:RequiredFieldValidator runat=server ControlToValidate=SomeTextBox ErrorMessage="Name is required."> *
</asp:RequiredFieldValidator>
I have added the "ValidationProperty" to the composite textbox, but the RequiredFieldValidator (or any ofthe other validators) don't seem to recognize the textbox or the data coming from it. The textbox (and all other custom CompositeControls I built) inherit CompositeControl, have the attribute "ValidationProperty" set to the public property that exposes the data element of the control, and contain a "string ValidationGroup" property as well.
Question: Does anybody know what else or what specifically a Composite Control requires to play nice with the ASP.NET validators (I'd prefer avoiding including instances of all the validator types in the composite control, unless of course that is necessary)?
I last looked into this in about 2005, but from what I remember, there are two separate issues here:
How the client-side validation code finds the value to validate
How the server-side validation finds the value to validate
Setting ValidationProperty is only going to affect the server-side validation, if I'm not mistaken. If you haven't already, check whether the IsValid property of the validators is set to false on the server side after you post back.
This may have changed, but I think it was also true that validation controls could only validate controls that had the same naming container as they did.
On the client side, I'm a little hazier, but I think the situation is still that the validation script will look for an element with the client ID corresponding to the server ID it was told to look for, and look for a value property on that. If you're not exposing one on the client side, it's not going to be able to find anything to validate.
If you have time, it'll help a lot to step through the validation javascript. That'll teach you a LOT about how client-side validation finds the values that it validates.
Hope this helps.
i am using multiple textboxes in webform and i would to validate each textbox by one validator in which validation like as any textbox does not empty and check the datatype of the input string.For this what of validator i can use and how?
jQuery.Validate is a good one for client side validation and DataAnnotations can be useful for server side validation.
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 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
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.