Click here to Skip to main content
15,886,796 members
Articles / Web Development / HTML
Article

Fire CustomValidator or RegularExpressionValidator ClientValidationFunction always!

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
22 Mar 2005 65K   15   5
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:

JavaScript
<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


Written By
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 Pin
acl12327-May-08 16:34
acl12327-May-08 16:34 
If you're making anything professional then the .NET validators won't work - they are a bug ridden mess.
I suggest purchasing Peter Blum's validation package.
Generalanother way to do this Pin
sh7773-Apr-05 18:59
sh7773-Apr-05 18:59 
GeneralRe: another way to do this Pin
Rooc3-Apr-05 21:01
Rooc3-Apr-05 21:01 
GeneralRe: another way to do this Pin
Shannon Deminick9-May-06 17:17
Shannon Deminick9-May-06 17:17 
GeneralRe: another way to do this Pin
oldefezziwig12-Jan-10 4:24
oldefezziwig12-Jan-10 4:24 

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

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