Click here to Skip to main content
12,293,873 members (63,412 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked

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.


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

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]);

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!


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralGet Peter Blum's validation Pin
Andrew C Lawrence27-May-08 16:34
memberAndrew C Lawrence27-May-08 16:34 
Generalanother way to do this Pin
sh7773-Apr-05 18:59
membersh7773-Apr-05 18:59 
GeneralRe: another way to do this Pin
Rooc3-Apr-05 21:01
memberRooc3-Apr-05 21:01 
GeneralRe: another way to do this Pin
ShannonD9-May-06 17:17
memberShannonD9-May-06 17:17 
GeneralRe: another way to do this Pin
oldefezziwig12-Jan-10 4:24
memberoldefezziwig12-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.

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