Click here to Skip to main content
Click here to Skip to main content

Fire CustomValidator or RegularExpressionValidator ClientValidationFunction always!

, 22 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Override the WebUIValidation.js function to fire the custom or regular expression validator even when the field is empty.

Introduction

Ever used a custom or required field validator and found that they don't fire when the field is empty? This simple "hack" will enable you to fire the clientvalidationfunction or evaluate the regular expression even when the fields are empty.

At the bottom of your page, (you can use the Page.RegisterStartupScript for this) add the following two functions:

<script language="javascript">

//OVERRIDE THE NORMAL VALIDATOR FRAMEWORK
function CustomValidatorEvaluateIsValid(val) {
    var value = "";
    if (typeof(val.controltovalidate) == "string") {
        value = ValidatorGetValue(val.controltovalidate);
        //make it fire always
        //if (ValidatorTrim(value).length == 0)
        //    return true;
    }
    var args = { Value:value, IsValid:true };
    if (typeof(val.clientvalidationfunction) == "string") {
        eval(val.clientvalidationfunction + "(val, args) ;");
    }        
    return args.IsValid;
}
function RegularExpressionValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    //make it fire always
    //if (ValidatorTrim(value).length == 0)
    //    return true;        
    var rx = new RegExp(val.validationexpression);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}
</script>

These functions also exist in the WebUIValidation.js of (in my case) the aspnet_cient\system_web\1_1_4322 folder, but because they are declared after the WebUIValidation.js include tag, these will get fired instead of the normal ones.

Note the commented lines. You could also comment these lines in the actual WebUIValidation.js file but an update of these files would overwrite this. That's why I've put them at the bottom of the WebForms using this feature. (Only tested in IE 6.0.)

Hope it helps someone!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Rooc
Web Developer
Netherlands Netherlands
Developer since 1998 and mainly focused on web development. Currently employed as technical architect.

Comments and Discussions

 
GeneralGet Peter Blum's validation PinmemberAndrew C Lawrence27-May-08 17:34 
Generalanother way to do this Pinmembersh7773-Apr-05 19:59 
GeneralRe: another way to do this PinmemberRooc3-Apr-05 22:01 
GeneralRe: another way to do this PinmemberShannonD9-May-06 18:17 
GeneralRe: another way to do this Pinmemberoldefezziwig12-Jan-10 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 22 Mar 2005
Article Copyright 2005 by Rooc
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid