There is a JavaScript function in SharePoint called "PreSaveAction" that is fired just before the item is validated and saved. You can use this function to call your function. For example:

function PreSaveAction(){
        var itemTitle = $("input[title='GUID']").val();
        if (itemTitle.length < 1) {
                alert("The Title Field Cannot Be Blank");
                $("input[title='Total']").attr("style", "border: solid 1px #ff4c42 !important");
                return false;
        }
    }

Please note, you should do complete validation of the item because if the item fails server side validation your function will have run but the item will not be saved (causing the user to fix the validation error, hit save again, and running your function again). This may not be important if you're only doing validation, but it's worth noting.

Answer from Taran Goel on Stack Exchange
🌐
Stack Overflow
stackoverflow.com › questions › 50816811 › how-to-display-the-html-div-so-that-after-form-submit-the-error-messgae-is-displ
css - How to display the html div so that after form submit the error messgae is displayed automatically - Stack Overflow
The validation is working fine.I only want to show div class="error-message alert-danger"> <span>Sorry, unrecognized username or password.<a href="#">Have you forgotten your password?</a></span> </div> automatically after submitting wrong username and password ... CSS pseudo class and style the border of the input accordingly. You could also attach a pattern to the validation. Check: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation
🌐
CodePen
codepen.io › palimadra › pen › OVvbaY
Custom error messages (pure css)
Minimize HTML Editor · Fold All · Unfold All · <div class="info">Info message</div> <div class="success">Successful operation message</div> <div class="warning">Warning message</div> <div class="error">Error message</div> <div class="validation">Validation message 1<br>Validation message 2</div> !
🌐
Stack Overflow
stackoverflow.com › questions › 43457255 › show-div-when-error-is-occur
jquery - show div when error is occur - Stack Overflow
May 23, 2017 - The error div is never hidden. You are updating the HTML, which actually removes the previous error messages. To tackle these, you can do: $(document).ready(function() { // Hide the error div. $("#error_message").hide(); $('#buttonregister').click(function() { var title = $('#title').val(); var name = $('#name').val(); var phone = $('#phone').val(); // Clear the error message div.
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 391531 › how-do-i-change-the-text-content-of-a-div-for-a-comment-validation-error-message
html - How do I change the text content of a div for a comment validation error message? - WordPress Development Stack Exchange
July 7, 2021 - How do I change the message content to "Please enter your comment."? I'm guessing that the following line of code might do the trick but I don't know how to nest HTML code in my functions.php file. document.getElementById("comment-error").innerHTML = "Please enter your comment.";
🌐
Adobe
experienceleague.adobe.com › en › docs › experience-manager-65 › content › forms › html5-forms › customzing-errors-html5-forms
Customizing error messages for HTML5 forms | Adobe Experience Manager
June 23, 2024 - To customize the position of the error message, add a <div> tag for each error and warning field, position the <div> tag on the left or right, and apply css styles on the <div> tag.
Find elsewhere
🌐
CodePen
codepen.io › DobsonDev › pen › GgRJwv
Error, Success, Warning and Info Messages
</div> <div class="warning-msg"> <i class="fa fa-warning"></i> This is a warning message. </div> <div class="error-msg"> <i class="fa fa-times-circle"></i> This is a error message.
🌐
Code Boxx
code-boxx.com › home › simple custom error messages with pure css
Simple Custom Error Messages With Pure CSS
March 16, 2023 - That is actually all we need to create a custom error message, an HTML <div> with the notification message inside.
🌐
W3C
w3.org › WAI › WCAG22 › Techniques › client-side-script › SCR32.html
SCR32: Providing client-side validation and adding error text via the DOM | WAI | W3C
This technique applies to script used with HTML. The objective of this technique is to demonstrate the display of an error message when client side validation of a form field has failed. Anchor elements are used to display the error messages in a list and are inserted above the fields to be ...
🌐
Oracle
docs.oracle.com › html › E79064_01 › Content › Tutorials and Samples › Eg_Dynamic_display_error_msg_for_control.htm
Example - How to dynamically display an error message for a control
// this will cancel submission of the form if there are still "dynamic" errors function allowSubmit(){ String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)} var divs = document.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { var div = divs[i]; if (div.id.startsWith('message_')){ if (div.style.visibility == "visible"){ return false; } } } return true; } </script> </head> <OWD deploy dir>\WEB-INF\classes\templates\investigation\form.vm · This template contains the <form> tag of the rendered HTML page. Here, we will add an onSubmit function call within the <form> tag. This will suspend the submission of the form when the user clicks the Submit button. It will check if there are still "dynamic" errors within the screen.
🌐
Stack Overflow
stackoverflow.com › questions › 33406196 › call-the-div-displaying-error-message-only-when-error-occurs
Call the div displaying error message only when error occurs
October 29, 2015 - I want that the div should be called only when error occurs because it is causing problems to style error message. <script type="text/javascript"> $(document).ready(function() { $('#submit').click(function() { var title = $('#title').val(); var body = $('#body').val(); if(title.length<5) { $('#er').html('Error mesg'); return false; } if(body.length<500) { $('#er').html('error msg'); return false; } }); }); </script> <?php error_reporting('E_ALL ^ E_NOTICE'); if(isset($_POST['submit'])) { $title=$_POST['title']; $body=$_POST['body']; if (strlen($title) < 5) { $er = "Title must be of minimum 5 c
Top answer
1 of 2
7

A more pure jQuery approach would be as shown below

jQuery Code

if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
   var errName = $("#name"); //Element selector
   errName.html("Please enter name"); // Put the message content inside div
   errName.addClass('error-msg'); //add a class to the element
}

CSS:

.error-msg{
   background-color: #FF0000;
}

Update:
You can even combine the jQuery methods on any selector. Whenever we apply a jQuery menthod on any selector, it returns a "this" pointer, so we can combine multiple methods and apply them to a selector using a single statement. This is called "chaining"

Read more here: http://www.w3schools.com/jquery/jquery_chaining.asp

if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
       $("#name").html("Please enter name")
                 .addClass("error-msg"); // chained methods
    }
2 of 2
0
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
   var errName = $("#name"); //get element by ID
   errName.append("Please enter name"); //append information to #name
   errName.attr("style", "background-color: red;"); //add a style attribute
}

Edit: or you could do it like so:

if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
   var errName = $("#name"); //get element by ID
   errName.append("Please enter name"); //append information to #name
   errName.attr("class", "alert"); //add a class to the element
}

Then you will have an .alert class. This makes it possible for you to use this class in your CSS file.

.alert {
   background-color: #FF0000;
}
🌐
MP4Moviez
pakainfo.com › home › javascript › display error message in div using javascript – how to display error without alert box using javascript?
Display Error Message In Div Using Javascript - How To Display Error Without Alert Box Using JavaScript? - Pakainfo
display error message in div using javascript validation is one of the most appealing part in web development where you can display errors messages to users. Contents Toggle · In this tutorial, i am going to see how to display an error message beside an input in JavaScript. index.html ·
🌐
Codeconvey
codeconvey.com › home › error message css style example with demo
Error Message CSS Style Example with Demo | Codeconvey
August 28, 2023 - You just need to wrap your “error message” in a div tag with the class name "error". You can add any further elements inside this tag. Therefore, a basic HTML for an error message is as follows: