AJAX not showing validation error message

For the past two days, I've been grappling with an issue and can't seem to pinpoint where it's coming from.

After leaving the textbox, the Ajax call functions correctly and returns results as either true or false, triggering the success function. However, the image and error text are not appearing next to the textbox. Despite receiving a "Must be under 50 characters" message when entering more than 50 characters in the textbox, no message displays when typing in an existing username.

What could I be overlooking? Any suggestions?

I am using a DevExpress Text Box

Html.DevExpress().Label(
        edtSettings =>
        {
            edtSettings.ControlStyle.CssClass = "label";
            edtSettings.Text = "User Name:";
            edtSettings.AssociatedControlName = "UserName";
        }
    )
    .Render();
    Html.DevExpress().TextBox(
        edtSettings =>
        {
            edtSettings.Name = "UserName";
            edtSettings.ControlStyle.CssClass = "editor";
            edtSettings.ShowModelErrors = true;
            edtSettings.Width = 100;
            edtSettings.Properties.ValidationSettings.Assign(IserValidationHelper.UserNameValidationSettings);
            edtSettings.Properties.ClientSideEvents.Validation = "OnNameValidation";
            edtSettings.ControlStyle.BackColor = System.Drawing.Color.LightYellow;
        }
        )
        .Bind(DataBinder.Eval(IserUser, "UserName"))
        .Render();

This is the JavaScript code I have:

<script type="text/javascript">

function OnNameValidation(s, e) {

    if (e.value == null)
        e.isValid = false;

    $.ajax({
        type: 'POST',
        url: '/Admin/CheckUsername',
        dataType: 'json',
        data: { userName: e.value },
        error: function () { alert("error"); },
        success: function (Data) {
            if (Data.result == true) {
                e.isValid = false;
                e.errorText = "User Exits";
            };
        }
    });

    var name = e.value;
    if (name == "")
        e.isValid = false;

    if (name.length > 50) {
        e.isValid = false;
        e.errorText = "Must be under 50 characters";
    }

}

This is the method within my controller:

[HttpPost]
    public ActionResult CheckUsername(string userName)
    {
        bool status = WebSecurity.UserExists(userName);
        return Json(new { result = status });
    }

Answer №1

After resolving the issue with my $.ajax call, I made sure to include the setting for async (async:false,) since the default value is true. This adjustment has resolved the problem and now everything is functioning correctly.

function OnNameValidation(s, e) {

    if (e.value == null)
        e.isValid = false;

    $.ajax({
        type: 'POST',
        url: '/ISERAdmin/CheckUsername',
        dataType: 'json',
        async:false,
        data: { userName: e.value },
        error: function () { alert("error"); },
        success: function (Data) {
            if (Data.result == true) {
                e.isValid = false;
                e.errorText = "User Exists";
            };
        }
    });

    var name = e.value;
    if (name == "")
        e.isValid = false;

    if (name.length > 56) {
        e.isValid = false;
        e.errorText = "Must be under 56 characters";
    }

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Angular Node server is responding with the error message "You have entered 'undefined' instead of a stream."

Using angular 9 + universal has been smooth sailing until I encountered an issue after building the app with npm run build:ssr and attempting to run it using node: node dist/app/server/main.js. The error message that popped up in the terminal was: Node ...

Is it possible to include multiple spyObjs within a beforeEach block?

In the process of testing an Angular 1 application using Jasmine, I have encountered a dilemma. My question is, can two spies be created for two different services within the same beforeEach statement? Currently, I have managed to make the first spy work ...

Strategies for preserving context throughout an Ajax request

In my project, I am looking to implement an Ajax call that will update a specific child element within the DOM based on the element clicked. Here is an example of the HTML structure: <div class="divClass"> <p class="pClass1">1</p> &l ...

Ways to activate an event based on the dimensions (width/height) of

Exploring ways to implement an if statement based on specific width/height values using this code example. Check out the code snippet here My approach: <p id="confirmation">Try again!</p> <script> if (new dynamicSize.width() < ...

Revisiting Dynamic URL Parameters with React Router and Express

After setting up my React router to navigate '/Article/1/' via a link, everything seems to be working fine. However, I encountered an issue when refreshing the browser as it no longer detects my Article component. MainApp.js import React from & ...

How can I use Angular to dynamically open a video that corresponds to a clicked image?

I need assistance with a functionality where clicking on an image opens a corresponding video on the next page. The issue is that all images have the same ID, making it difficult to link each image to its respective video. Below is the data I am working ...

Automatically insert content into a div following the execution of an AJAX delete function using jQuery

I've been working on a feature to display an auto-populated message in the results div when a user deletes the last item from their favorites list, indicating that it is empty. However, I've hit a roadblock and can't seem to make it work. H ...

Setting up WebPack for TypeScript with import functionality

A tutorial on webpack configuration for typescript typically demonstrates the following: const path = require('path'); module.exports = { ... } Is it more advantageous to utilize ES modules and configure it with import statements instead? Or is ...

Completion of TypeScript code is not working as expected, the variable that is dependent upon is not

Looking for assistance with creating code completion in TypeScript. Variable.Append1 Variable.Append2 Variable.Append3 I have defined the following class: class Variable { Append1(name: string){ if (name == undefined) ...

What is the best way to send props to a React component?

Sorry for the inconvenience of asking for help with finding an issue in my code, but I'm facing challenges while learning React. I am attempting to pass a variable named hashRoute to a component in react. However, every time I try to access the prop ...

JavaScript counter that starts at 1 and increments with each rotation

I am working with an array of image IDs. let images = ['238239', '389943', '989238', ... ]; let max = images.length; The array begins at index 0 and its size may vary. For instance, if there are 5 images in the array, the i ...

The editor is locked and choices are displayed in a vertical orientation

I'm currently experimenting with using draft js in my project to create a wysiwyg editor. However, I've encountered an issue where the editor appears vertically instead of horizontally when I load the component. Any idea why this might be happen ...

Encountering a "DOM Exception 11: InvalidStateError" when attempting to use websocket.send

I encountered the following error message: DOM Invalidate exception 11 This error is coming from the code snippet below, but I'm having trouble identifying the root cause. /*The coding style may appear pseudo-stylish with potential syntax errors*/ ...

The entry '0-0' already exists for the key 'local_part', please enter a unique value

Creating a simple API to handle GET, POST, DELETE, and UPDATE requests. The GET method is functioning correctly, but encountering an issue with the POST method. When attempting to post data, an error is being encountered: error: Error: ER_DUP_ENTRY: ...

Exploring the Method of Accessing data-* Attributes in Vue.js

I have a button that, when clicked, triggers a modal to open. The content displayed in the modal is determined by the data attributes passed to the button. Here is my button: <button class="btn btn-info" data-toggle="modal" data-t ...

Tracking page views through ajax requests on Google Analytics

I have implemented a method to log page views through ajax action when the inner page content is loaded. However, I am facing an issue where the bounce rate data is not getting updated and always shows 0%. The default Google Analytics page view is logged ...

Tips for sending an XML string in an ajax request to an MVC controller?

I have a challenge with posting an XML string to an MVC controller using an Ajax call. The MVC controller only accepts a string parameter, and I'm struggling to successfully process the request. How can I effectively send the XML string to the Control ...

Combine identical arrays of object keys into one unified array

I am striving for this particular output [ productId:106290, productserialno:[{ "12121", "212121" }] ] ...

What is the best way to alter the order of the .map function in JavaScript to display in ascending or descending order?

I currently have 25 songs in my Spotify playlist, with the possibility of more being added in the future. The songs are numbered from 1 to 25. However, the map() function I use only displays the top 10 songs (1 to 10). When a new song is added, it is assig ...

What is the process for transferring the "event" object to the functional form of "this.setState()"?

Utilizing the functional form of this.setState() where a function returning an object is passed. When attempting to bind onChange on an input and using this... onInputChange = event => { this.setState(() => ({ ...this.state, [ev ...