user3340627
user3340627

Reputation: 3143

CustomFieldValidator ClientValidationFunction not firing and no error

I've added a CustomValidator ClientValidationFunction to validate a text field in my web application, however, this validation function doesn't fire at all. Here's my validation function:

function isValidYear(sender,args)
        {
            alert('Me here');
            var year = document.getElementById('MainContent_frmArticle_Source_txtYear').value;
            var currentdate = new Date();
            var regex = new RegExp("^\d{4}$");
            if (year > 1 && year <= currentdate.getFullYear() && regex.test(year)) {
                alert('IsValid');
                args.IsValid = true;
            }
            else
            {
                alert('Is Not Valid');
                args.IsValid = false;
            }

// I added these alerts just to test if the function is being fired, but they never show.

Here's my custom validator code:

<asp:TextBox ID="txtYear" runat="server" widtth="20%"></asp:TextBox>
<asp:CustomValidator runat="server" ID="CustomValidateYear" ClientValidationFunction="isValidYear" ControlToValidate="txtYear" ErrorMessage="Please enter a valid year" Display="Static" ForeColor="Red" ></asp:CustomValidator>

More Info:
I'm using Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.114.0 and at the first run after adding the validator I got an error with "unobtrusive validation" so I followed steps from a website (that I can't remember which one it was) to install packages to my project:

<package id="AspNet.ScriptManager.jQuery" version="2.1.4" targetFramework="net45" />
  <package id="jQuery" version="2.1.4" targetFramework="net45" />
  <package id="Microsoft.AspNet.ScriptManager.MSAjax" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.ScriptManager.WebForms" version="5.0.0" targetFramework="net45" />

Please Note that I do not want to set the unobtrusive validation to None as mentioned by most of the posts on SO.

Upvotes: 1

Views: 1105

Answers (1)

makison
makison

Reputation: 383

Code you posted works, only what I noticed that you missed closing bracket } in your js function isValidYear, and check if you put it inside <script type="text/javascript"></script> so it will look like

<script type="text/javascript">

    function isValidYear(sender, args) {
        alert('Me here');
        var year = document.getElementById('MainContent_frmArticle_Source_txtYear').value;
        var currentdate = new Date();
        var regex = new RegExp("^\d{4}$");
        if (year > 1 && year <= currentdate.getFullYear() && regex.test(year)) {
            alert('IsValid');
            args.IsValid = true;
        }
        else {
            alert('Is Not Valid');
            args.IsValid = false;
        }
    }
</script>

Also please notice CustomValidator won't fire on empty text you need to add RequiredFieldValidator

btw you can simply replace yours document.getElementById('MainContent_frmArticle_Source_txtYear').value

with

args.Value

Upvotes: 2

Related Questions