When I first started working on my validation using jquery/javascript, it was not very effective. However, with the implementation of a new css class and updates to my jquery validation, things have greatly improved.
I've been focusing on enhancing user input validation in different forms within a web portal based on the python web framework cherrypy, particularly through jquery. Initially, a single check was done for all inputs failing validation, turning borders and labels red. Now, individual checks are performed for each required field, with error messages showing only for fields that fail validation.
My next goal is to reset the css styles back to their defaults before conducting the validation check. This would prevent fields corrected by users from being marked with red borders and labels. Despite this, any fields still failing validation during submission will be highlighted in red again.
Is there a way to achieve this without multiple lines of code at the beginning of the submit function?
document.getElementById('elementHere').style.borderColor = "black";
document.getElementById('elementHere').style.color = "black";
$( document ).ready(function() {
//snip
$("#btnSubmit").click(function(){
$("#dlgmessage").html("Processing...");
$("#dialog-message").dialog("open");
//New Validation
var validated = "yes";
var msg = "One or more fields do not meet the validation requirements:<ul>";
//More jquery validations
//Make sure basic inputs are filled in
//if (
// Clean($("#txtIndex").val()) === "" ||
// Clean($("#txtSourcetype").val()) === "" ||
// Clean($("#txtUseCase").val()) === "" ||
//more validation conditions
//){
//Error message display
//console.log("Missing Required Fields");
// return;
//}
//Successful validation
var postdata = {
record_id: Clean($("#txtID").val()),
splunk_index: Clean($("#txtIndex").val()),
} ;
$.post( "/submit", {data:JSON.stringify(postdata)},
function( data ) {
var msg = data;
},
'text'
);
});
});
I'm looking for a solution where the css can be reset automatically, but if specifying each element and its default color is necessary, that's also acceptable.
EDIT:
After reevaluating my previous code which was quite basic, I made significant improvements to make it more dynamic and avoid extensive if statements. The latest version seems promising, as classes are added to html elements, yet the styling changes like applying red color to invalid fields are missing. The earlier method with
$(field_id).parent().addClass("error");
worked for me initially, but now I want only failed fields to turn red.
Below is my updated code
Thank you
$( document ).ready(function() {
$("#btnSubmit").click(function(){
$("#dlgmessage").html("Processing...");
$("#dialog-message").dialog("open");
var postdata = {
splunk_host: Clean($("#txtSplunkHost").val()),
ip: Clean($("#txtIP").val()),
} ;
//Validation process
//---------------------------------------------------------------------------------------------------
$("#dialog-message").dialog({
modal: true,
autoOpen: false,
position: { my: "top", at: "top", of: $("#page-inner") },
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
});
/*========================================
Errors
========================================*/
.error input {
border: 2px solid red;
}
.error label {
color: red;
}
<div class="form-group">
<label id="txtSplunkHost_label">Splunk Host (*)</label>
<input class="form-control" placeholder="Splunk Host" id="txtSplunkHost" value="" maxlength="255" autofocus="">
</div>
<button type="button" class="btn btn-default" id="btnSubmit">Submit</button>