JS验证-自定义规则? [英] JS Validate - custom rules?

查看:92
本文介绍了JS验证-自定义规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery Validate插件-如何创建简单的自定义规则?

Possible Duplicate:
jQuery Validate Plugin - How to create a simple, custom rule?

我正在使用js validate,我想知道是否有可能建立自己的规则?

Im using js validate and i was wondering if it was possible to st up your own rules?

我想在验证之前检查数据库中是否存在用户名.为此,我需要从js方法中返回true或false.

I want to check whether a username exists in my db before validating. To do this I need to return true or false from a method in js.

如何将其添加到js验证中?

How can I add this in to js validate?

推荐答案

尝试使用js中的php进行抓取以提高安全性,因为客户端可以读取js文件.

try fetching with php in js for better security as js files can be read by the client.

示例:

$.validator.addMethod("uniqueUserName", function(value, element) {
  $.ajax({
      type: "POST",
       url: "php/get_save_status.php",
      data: "checkUsername="+value,
      dataType:"html",
   success: function(msg)
   {
      // if the user exists, it returns a string "true"
      if(msg == "true")
         return false;  // already exists
      return true;      // username is free to use
   }
 })}, "Username is Already Taken");

参考: jQuery验证器和使用AJAX的自定义规则

这篇关于JS验证-自定义规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆