开发者

How to validate a form with jQuery?

开发者 https://www.devze.com 2023-01-01 10:09 出处:网络
I tried to find the answer in other questions but actually nothing worked -.- Somehow I hate JavaScript ... Anyway! My Code looks like:

I tried to find the answer in other questions but actually nothing worked -.- Somehow I hate JavaScript ... Anyway! My Code looks like:

function validate()
{
 if ($(":input").length == 0) 
 {
  $(":input").addClass("notvalid");
  return false;
 }
 else
 {
  $(":input").removeClass("notvalid");
  return true;
 } 

 return true;
}

$(":input").blur(validate());
$(":input").keyup(validate());

$("#customForm").submit(function(){
 i开发者_运维技巧f(validate()){
  return true;
 }
 else
 {
  return false;
 }
});

I just want to test every tag to be not empty. The test should be done when the focus is lost or after every key type. And of course after the submit button has been clicked.

It doesn't work. Firefox error console says something like: unknown pseudoclass or pseudoelement 'input'. What does this mean?


You can alway use AJAX to validate your fields if you are more familiar with server side scripting like PHP.

Back to JS here what i think you want to accomplish:

$("input:text").bind("blur, keyup", function(){
  if($(this).val().length > 0){
    //Valid not empty
    $(this).removeClass("notvalid");     
  }else{
    //invalid empty
    $(this).addClass("notvalid");
  }
});

$("#customForm").submit(function(){
  $("input:text").keyup(); //triggers the keyup event on input fields 
                         //which then validates your fields according the
                         //code above.

  if($("form .notvalid").length > 0){
    //You got some invalid fields in form

    return false;
  }else{
    //All fields are valid continue

    return true;
  }

});

it may look a whole different then your approach but if you want to learn jQuery is better to get familiar with it. I commented some of the process, but if you have any doubt of the methods or functions used in code above you just google it. Good Luck!


The error says the jQuery doesn't know what elements to select with :input. Just replace :input but input:text in your selectors. You could also take a look at the jquery.validate plugin which will simplify your validation logic. There are many demos on the site that should help you get started.


UPDATE:

My mistake. There's :input selector according to the documentation.


Instead of:

function validate()
{
 if ($(":input").length == 0) 
 {
  $(":input").addClass("notvalid");
  return false;
 }
 else
 {
  $(":input").removeClass("notvalid");
  return true;
 } 

 return true;
}

Use:

function validate()
{

$(":input").each(function(){
  if ($(this).val() === '')
  {
    $(this).addClass("notvalid");
    return false;
  }
  else
  {
    $(this).removeClass("notvalid");
    return true;
  } 
});

 return true;
}


If your using jQuery already you should really use jquery.validate plugin like Darin said.

This will make you code much cleaner and easier to add standard validation rules like valid email, URL, min length, max length etc. Also removes the need of having to add and remove classes at does it for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消