10 examples of 'website validation in jquery' in JavaScript

Every line of 'website validation in jquery' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
85function validate(form) {
86 var i = 0;
87 jQuery("input.required").each(function() {
88 if(jQuery(this).val() == '') {
89 jQuery(this).focus();
90 return false;
91 }
92 i++;
93 });
94 if(i < jQuery("input.required").length) {
95 return false;
96 } else {
97 return true;
98 }
99}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

203function validateForm () {
204 var valid = true; /* "true" is all good news */
205 if ($.fn.validate === undefined)
206 return valid;
207
208 var validator = $top.validate();
209
210 // Look at the inputs for each non-hidden step in our form
211 $("div.ui-dform-step:not(:hidden) input", $top).each(function () {
212 var tvalid = validator.element(this);
213 if (tvalid === undefined) // Agnostic? Force a value!
214 tvalid = true;
215 valid &= tvalid;
216 $.dbgpr("validateForm:", this.name, ":", tvalid);
217 })
218
219 $.dbgpr("validateForm:", this.name, " is ", valid);
220 return valid;
221}
26function ajaxValidationCallback(status, form, json, options){
27 if (window.console)
28 console.log(status);
29
30 if (status === true) {
31 alert("the form is valid!");
32 // uncomment these lines to submit the form to form.action
33 // form.validationEngine('detach');
34 // form.submit();
35 // or you may use AJAX again to submit the data
36 }
37}
79function validateForm(formId) {
80 // This function deals with validation of the form fields
81 var y, i, valid = true;
82 y = document.forms[formId].getElementsByTagName("input");
83 // A loop that checks every input field in the current tab:
84 for (i = 0; i < y.length; i++) {
85 // If a field is empty...
86 if (y[i].getAttribute("pattern")){
87 var pattern = new RegExp(y[i].getAttribute("pattern"));
88 var match = y[i].value.match(pattern);
89 if (!match){
90 // add an "invalid" class to the field:
91 y[i].className += " invalid";
92 // and set the current valid status to false
93 valid = false;
94 }
95 } else if (y[i].getAttribute("type")=='checkbox' && y[i].getAttribute("required") && !y[i].checked){
96 valid = false;
97 }
98 if (!valid){
99 y[i].focus();
100 }
101 }
102
103 return valid; // return the valid status
104}
205function validateForm(form) {
206 if (form.elements[0].value.length === 0) {
207 const $form = $(form);
208 if (!$form.hasClass('errors')) {
209 $form
210 .addClass('errors')
211 .find('input')
212 .addClass('error')
213 .attr('aria-invalid', 'true');
214 $(
215 `<div class="description" >${Drupal.t(
216 'Please provide a name for the button group.',
217 )}</div>`,
218 ).insertAfter(form.elements[0]);
219 }
220 return true;
221 }
222 return false;
223}
16function validateClientSide() {
17 var isValid = true;
18
19 // Remove previous error messages, if they exist.
20 if ($('label#usernameField span.error').length) {
21 $('label#usernameField span.error').remove();
22 }
23
24 if ($('label#passwordField span.error').length) {
25 $('label#passwordField span.error').remove();
26 }
27
28 // Deal with each field separately.
29 if ($('input#username').val() == '') {
30 $('input#username').after('<span class="error">Provide a username.</span>');
31 isValid = false;
32 }
33
34 if ($('input#password').val() == '') {
35 $('input#password').after('<span class="error">Provide a password.</span>');
36 isValid = false;
37 }
38
39 if ($('input#password').val().length != 4) {
40 $('input#password').after('<span class="error">Password must be 4 alphanumeric characters</span>');
41 isValid = false;
42 }
43
44 return isValid;
45}
646function validate($form) {
647 values = {};
648 $.each($form.serializeArray(), function(i, field) {
649 values[field.name] = field.value;
650 });
651 values['_token'] = CSRFToken;
652
653 $.ajax({
654 url: validationUrl,
655 method: 'POST',
656 data: values,
657 success: function(data) {
658 $form.submit();
659 },
660 error: function(err) {
661 console.log(err);
662 $('.error-message').text('');
663 $('.text-input, .text-area, .chosen-container').removeClass('error');
664
665 $.each(err.responseJSON.errors, function(fieldName, errors) {
666 var $field = $('#'+fieldName);
667 $field.addClass('error');
668 $field.siblings('.error-message').text(errors[0]);
669 });
670 }
671 });
672 }
17function validateForm()
18{
19 var message = "";
20 var returnValue = true;
21 var fm = document.getElementById("fm");
22 if(fm.firstName.value == "")
23 {
24 message += "- The field First Name is Mandatory \n";
25 returnValue = false;
26 }
27 if(fm.lastName.value == "")
28 {
29 message += "- The field Last Name is Mandatory \n";
30 returnValue = false;
31 }
32 if(fm.email.value == "")
33 {
34 message += "- The field Email is Mandatory \n";
35 returnValue = false;
36 }
37 if(!returnValue)
38 {
39 message = "Please correct the following errors:\n\n" + message;
40 alert(message);
41 }
42 return returnValue;
43}
177function validateForm() {
178 if ($("input#username").val() && $("input#password").val()) {
179 return true;
180 } else {
181 return false;
182 }
183}
76function validate() {
77 $("#service-config-form").bootstrapValidator({
78 message: "This value is not valid",
79 feedbackIcons: {
80 valid: "glyphicon glyphicon-ok",
81 invalid: "glyphicon glyphicon-remove",
82 validating: "glyphicon glyphicon-refresh"
83 },
84 fields: {
85 weight: {
86 validators: {
87 regexp: {
88 regexp: /^(-?\d+)?$/,
89 message: $.i18n.prop("weight-should-be-integer")
90 }
91 }
92 }
93 }
94 });
95 $("#service-config-form").submit(function(event) {
96 event.preventDefault();
97 });
98}

Related snippets