0

How to validate my all fields by using jquery ?

if validation fails,i want to redirect to new page and list out all validation failed fields.If it is success i will do insert operation.

Example

<input class="textbox validate"type="text"> 

<input class="textbox validate"type="text"> 


//validate the all the field with having "validate" class
$(".validate").each

I am using MVC-3 but i want to do in custom j-query logic. I am a new person in j-query.

Thanks in advance !

Rakesh patanga
  • 832
  • 9
  • 25
SaravanaKumar T
  • 105
  • 1
  • 5

1 Answers1

0

Assuming you have a single function for validation:

function validate (text) {
  ...
  return true; //or false
}

Then one thing you can do:

var validationErrors = [],
    errorPageURL = "BASE URL for your error page";

$(".validate").each(function (index, element) { 

  if (!validate(element.val()) {
    validationErrors.push($(element).id);
  }
});

if (validationErrors.length === 0) {
  //Do your input magic
} else {
  window.location.replace(errorPageURL + "?errors=" + encodeURI(JSON.stringify(validationErrors)));
}

A few reference links:

Community
  • 1
  • 1
mlr
  • 886
  • 6
  • 15
  • 1
    Thanks mlr, I will try with yours and get back to you – SaravanaKumar T Aug 11 '14 at 07:47
  • This looks good. Sometimes I like to also push back the error as a jquery object and not just an id so I can add visual cues to the error like adding a red border around something without using another call that looks up the element to do something. – Scott Mitchell Aug 11 '14 at 12:49