Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6769977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:15:28+00:00 2026-05-26T15:15:28+00:00

I’m fairly new at creating custom unobtrusive validation, and am running into a weird

  • 0

I’m fairly new at creating custom unobtrusive validation, and am running into a weird problem.

This validation basically just checks the database (via Ajax) to see if the model and serial number combination already exists. The jQuery and Ajax seem to be behaving as expected, but my validation message appears as soon as my validation function is called.

Here is my jQuery:

$.validator.addMethod('newserialandmodel',
    function (value, element, parameters) {
        var modelNumber = $('#ProductInformation_ModelNumber').val();
        var serialNumber = value;

        var token = $('input[name=__RequestVerificationToken]').val();

        $.ajax({
            type: "POST",
            url: "/ajax/getmodelandserialexists/",
            data: ({ 
                    modelNumber: modelNumber, 
                    serialNumber: serialNumber, 
                    '__RequestVerificationToken': token 
                  }),
            cache: true,
            dataType: 'json',
            success: function (response) {
                if (response.exists) {
                    $.validator.messages.newserialandmodel = 
                        $.format($.validator.messages.newserialandmodel);
                }

                return !response.exists;
            }
        });

        return false;
    }
);

$.validator.unobtrusive.adapters.add(
    'newserialandmodel',
    function (options) {
        options.rules['newserialandmodel'] = options.params;

        if (options.message != null) {
            $.validator.messages.newserialandmodel = options.message;
        }
    }
);

Here is what the form element looks like:

<input type="text" value="" 
 name="ProductInformation.SerialNumber" id="ProductInformation_SerialNumber" 
 data-val-required="The Serial number field is required." 
 data-val-newserialandmodel="There is already a contract with the provided model and serial numbers." 
 data-val-length-max="30" 
 data-val-length="The 30 field requires no more than 30 characters." 
 data-val="true" class="input-validation-error">

When I step through, as soon as it hits the first line of the validation, the message is already shown:

There is already a contract with the provided model and serial numbers.

Also, even though the function returns true, it the message remains.

The rest of the function/Ajax works just fine, I just can figure out why it’s showing this message immediately, and why it stays there when I return true.

Any idea what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:15:29+00:00Added an answer on May 26, 2026 at 3:15 pm

    Thanks to Greg and Zero’s responses, it led me to playing around w/ the return value a bit more.

    The problem is that the return within my Ajax success function just returns the response back to the success function, not the validation function. So I had to change it to set a value within the Ajax success function, and return that.

    It’s also worth noting that I had to include async: false, as otherwise the validation function will return (the default value of false) before the Ajax call completes.

    $.validator.addMethod('newserialandmodel',
        function (value, element, parameters) {
            var modelNumber = $('#ProductInformation_ModelNumber').val();
            var serialNumber = value;
    
            var token = $('input[name=__RequestVerificationToken]').val();
    
            var isValid = false;
    
            $.ajax({
                type: "POST",
                url: "/ajax/getmodelandserialexists/",
                data: ({ 
                        modelNumber: modelNumber, 
                        serialNumber: serialNumber, 
                        '__RequestVerificationToken': token }),
                async: false,
                dataType: 'json',
                success: function (response) {
                    if (response.exists) {
                        $.validator.messages.newserialandmodel = 
                            $.format($.validator.messages.newserialandmodel);
                    }
    
                    isValid = !response.exists;
                }
            });
    
            return isValid;
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.