Validator.Validate() ?

Hello guys,

When I created a function to do custom validation I tried to use RegularExpressionValidator.Validate(). My code:


1       protected void EvaluateNewGateWay(object source, ServerValidateEventArgs args)
2        {
3            if (chBoxNewGW.Checked)
4            {
5                RegularExpressionValidator rev = new RegularExpressionValidator();
6                rev.ErrorMessage = "REV Error";
7                rev.ControlToValidate = "txtNewGW";
8                rev.ValidationExpression = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
9                rev.Text = "Rev Error";
11               rev.Validate();// Error here !!!
13               if (!Page.IsValid)
14                   args.IsValid = false;
16           }
18           args.IsValid = true;
19       }

  I got error at line 11:


Server Error in '/UI_test' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 35:             rfv.EnableClientScript = true;
Line 36: rfv.Enabled = true;
Line 37: rfv.Validate();
Line 38: "

Line 39: if (!Page.IsValid)

I thought I had created a "rfv" object, however....

Does anybody know the reason?

 Thx in advance,



5/8/2007 3:50:16 AM 93655 articles. 5 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 54

It is not neccessary to set Page.IsValid.

Page.Validate() calls all registered validators. One of the reasons for re-creating dynamic controls during PreInit is that during Init a Validator Control adds itself to the pages IValidators Collection. Then when validation is later called, via the framework or Page.Validate(), the page loops through all registerd validators calling validate on each - via Interface defined method.. In your case the rev is not added to the page so there is no event catchup (addition to IValidators collection). However, it is unneccessary as you will see.

Some controls such as Button Controls default to "CausesValidation=true". Therefore calling Page.Validate() is not required in their control events as it will already have been called.

Just check Page.IsValid to see if any failed validation. However, ensure that Page.Validate() will have been called either by via a control or manually.

You don't need an instance of a RegularExpressionValidator Control to validate text with a regular expression.

This is more appropriate (fully qualified for clarity) -

protected void EvaluateNewGateWay(object source, ServerValidateEventArgs args)
            args.IsValid = true;
            if (chBoxNewGW.Checked)
                args.IsValid = System.Text.RegularExpressions.Regex.IsMatch(txtNewGW.Text, "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");


Hope that helps some.


For the benefit of all users please mark any post answers as appropriate.
5/8/2007 5:25:38 AM

Similar Artilces:

Validators not Validating
Would anyone know why the validations on the page below wouldn't work. I am going through a book and learning and for some reason when I put in the last subroutine to insert the record the validation checks on the controls on the form no longer work.<%@ Import Namespace="System.Data.SqlClient" %><html><head><title>The Intranet Site</title><script runat="server" language="VB">Dim objConn As New SQLConnection("Server=XXXXXXX; database=Dorknozzle; uid=XXXXXXX; Pwd=XXXXXXXX;")Dim objCmd As SQLCommandDim objRdr As SQLDataReader  Sub...

To validate or not to validate
Hey people! Here is my situation - I've created a dynamic data website with one table. The fields of this table are mostly varchar() not NULL, and all fields are defined with default value. When trying to insert new row with empty values I get required validation on all fields. What would be the best way to enable this validation only on some of these fields, while using default values for the others? For example empty string for varchar or 0 for int, etc Thanks Michael no in this case it will be add what error its return when u add. .Net DeveloperWeb Site Design and W...

Web form and a gridview on the same page with data validators
I have a form where a user can submit a new customer and directly below that is a gridview where they can view the customer they just submited plus their other customers they have submited in the past. Now this gridview has the edit/update property so the user can modify their customer information. The whole idea is to have 1 single page where a user can do everything they need to without having to be linked to another page. On this page we can submit new customer info and view the status/info of existing customers and edit that info as needed. So far that works fine. I can submit the new c...

Page.Validate() not validating custom validators
Hi,when i call Page.Validate("myGroup") The custom validators in the validation group myGroup  are not validated, i.e.  validator.IsValid returns true even if the validator failed on  page submit. code snippet: Page.Validate("ToolsImplementation"); foreach (IValidator validator in this.Validators) { if (!validator.IsValid) { //take action on validation failure } }   the code works fine except if valida...

.net validation vs custom validation
Hello, Coming from a background of Classic ASP. I have a logic of validation which also does out security and account. Basically, we use a stored proceedure to validate and then also log success, failures, attempts etc Can this be done using .net validation or i have to build my own validation logic ? And how is this done ? thanks Ehi You can use Custom validator control Using the CustomValidator Control  Hosam Kamel Remember to click on Mark as answer on the post that helped you...

validate Textbox in Web User Control from web form?
ASP.NET 2.0 I have a web user control that contains a textbox.  I need to add some validation (like requiredfieldvalidator, comparevalidator, etc). I would normally add the validator to my web user control by placing the validator next to my textbox.  However, I need the validation message somewhere else on my web form, not near the user control. So how would I associate a validation control on my web form with the text box inside my web user control?  Can this be done; any alternatives out there? Thanks!   The error message should occur wherever you place the vali...

Web Form Validation
I am creating a dynamic form (from XML) and would like to use regular expression to validate. What is the best way to do this? -Tamim...

web form validation
Hi, is it possible to use validation contorls with Javascript disabled in the client browser. If not, what are the alternatives? Many thanks Gareth Sure, it works fine. The only difference is that validation does not update when then change a field clientside... it does however update when the users pushes a submit button (after a round trip to the server)Pandora Tomorrow Yes, but you must make sure that you test that the page is valid before you do it, e.g. If page.Isvalid() Then ...... End IfIntelligence is a burden.Jagdip Singh AjimalManchester (and yes, I am a Manc...

web forms with validation
I have a web form that has several required fields, and then a sumit and clear button. What I'm having trouble with, is how to clear the form if all the required fields don't have content. Thanks Hi, did you set the CausesValidation property to False of the clear button? Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons. Thanks, figured it was something simple...

About web Form validation?
Hi all, I have a textbox, and I want the data in it is decimal or integer. How can I build it with Web Form validator? Is there any way to build a textbox that only display numeric data? That means when you press the key is not '0...9', the textbox is not display. Thanks alot for support! The life is beautiful! you can use a regularexpressionvalidator and only allow numeric. ie: <asp:TextBox id="tb" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ControlToValidate="tb" ValidationExpression="^\d*" T...

Defaultbutton and Validators causing form to submit when it is not valid.
In Internet Explorer, when you have a single textbox, a defaultbutton on the form, and a validator, by pressing enter in the textbox the first time (if it is invalid), the validator fires and the form doesn't submit.  However, this sets __defaultFired to true, therefore the next time you click enter, even though the textbox is not valid, the page submits.  In this case, the page_load executes and ispostback is true, however the button onclick server side function does not fire. I have included an aspx page below that experiences this bug.  The bug can be witnessed by opening ...

Validation controls are triggereing other forms validation controls?
Can someone please look at this an explain whats going on. When you click on subscribe it triggers my registration form validation controls. I'not sure what "subscribe" is supposed to do, but if you dont want it to cause validation, set the CausesValidation property to false.Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: Make sure the feild is an email address Hey coolkidsonly, your initial posting didn't show us anything to look at. We are just guessing here without the specifics. However, m...

Validating States dropdownlist with all other validation controls on form?
I have States dropdownlist  starts with "--" ListItem and 50 States. If user select "--" first ListItem, I need validate at client. Right now I am doing code behind side validation. But I have other controls on the form for validation, So I need to validate all at time. on submit button click.   Hi Srikanth, Place a required field for states dropdown list and set the "intial value" property of required field validator to "--". On Submit click validation is done. Above is the solution if i am not misunderstood you.   Regards, Pra...

validating .net validation controls using javascript
 I need to validate multiple .net validation groups(a normal group would consist of RequiredFieldValidators, RegularExpressionValidators, ValidationSummary, etc) using javascript.  I am using Page_ClientValidate("GroupName") to validate the groups.  The problem arises when i validate multiple groups.  The validation summary and errors only appear for the last group validated or nothing appears if the last group validated returns true.  I want all validation summaries and errors to appear for all groups that return a false validation.  I'll never va...

Web resources about - Validator.Validate() ? -

Resources last updated: 11/28/2015 8:18:34 PM