开发者

validating email address using AJAX

开发者 https://www.devze.com 2023-03-22 18:36 出处:网络
Is there an easy way to check an email address is valid using Ajax? I\'m using the sfValidatortEmail widget currently and this only shows an error开发者_StackOverflow社区 message on submitting the for

Is there an easy way to check an email address is valid using Ajax? I'm using the sfValidatortEmail widget currently and this only shows an error开发者_StackOverflow社区 message on submitting the form.

Thanks


I'm assuming you know how to write ajax requests. In the action you'd have something like

if ($request->isXmlHttpRequest()) {
  // handle ajax check
  try{
    $validator = new sfValidatorEmail();
    $validator->clean($request->getParameter('email'));
    // good to go
  } catch(sfValidatorError $e){
    // invalid email
    $this->getResponse()->setStatusCode(400);
  }
} else {
  // handle normal post
}


You'd need to add some Javascript/Jquery to fire an Ajax request to check the email, possibly linked to when the user clicks out of the email input box (blur function). This would be separate from your form class (assuming that the form has other elements too), but you could use the same action to handle the request if you wish:

if ($request->isXmlHttpRequest()) {
  // handle ajax check
} else {
  // handle normal post
}
0

精彩评论

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