Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <form action="" id="registerForm" method="post">
  请输入用户名: <input type="text" name="userName" value="12345"><br>
  请输入密码: <input type="password" name="password" value="123456"><br>
  请输入手机号码: <input type="phone" name="phoneNumber" value="13751146374"><br>
  
  <input type="submit">
</form>
              
            
!

CSS

              
                
              
            
!

JS

              
                let registerForm = document.getElementById("registerForm");

const Rules = {
  longerThan6: val => val && val.length >= 6,
  isPhone: val => val && /(^1[3|5|8][0-9]{9}$)/.test(val),
  notEmpty: val => val && val.length != 0
};
let { longerThan6, isPhone, notEmpty } = Rules;

let validate = () => {
  let validator = new Validator();

  // 一次添加多个规则
  validator.add(registerForm.userName, [
    {
      validateFunc: val => val.length >= 6,
      errorMessage: "用户名长度不可小于六位"
    },
    { validateFunc: notEmpty, message: "用户名不能为空" }
  ]);

  // 一次添加单个规则
  validator.add(registerForm.password, longerThan6, "密码长度必须大于6位");
  validator.add(registerForm.phoneNumber, isPhone, "电话号码格式错误");

  // 开始校验,成功则返回false,失败则返回对应的errorMessage。
  return validator.start();
};

registerForm.onsubmit = () => {
  let errorMessage = validate();
  if (errorMessage) {
    alert(errorMessage);
    return false;
  }
};

class Validator {
  rules = [];
  constructor() {
    this.rules = [];
  }

  /**
   * 该方法负责添加验证规则。
   * 该函数接受三个参数,第一个参数是input元素(Element)
   * 第二个参数可能是数组或函数,若第二个参数是数组,忽略第三个参数,
   * 且该数组的元素结构必须是{validateFunc: Function, errorMessage: String}。
   *
   * @param {Element} inputElement
   * @param {Array | Function} rules
   * @param {String} message
   * @memberof Validator
   */

  add(inputElement, rules, errorMessage) {
    let val = inputElement.value;

    // 如果第二个参数是函数,那就是用于验证的函数咯。
    if (typeof rules === "function") {
      let validateFunc = rules; // 没啥意义的赋值,为了你更好的理解。

      this.rules.push({
        value: val,
        validateFunc: validateFunc,
        errorMessage: errorMessage
      });
      return;
    }

    // 如果第二个参数是数组,那就是一组规则,忽略第三个参数。
    if (Array.isArray(rules)) {
      for (let rule of rules) {
        this.rules.push({
          value: val,
          validateFunc: rule.validateFunc,
          errorMessage: rule.errorMessage
        });
      }
      return;
    }
  }

  start() {
    for (let rule of this.rules) {
      let { value, validateFunc, errorMessage } = rule;
      if (!validateFunc(value)) {
        return errorMessage;
      }
    }
    return false;
  }
}

              
            
!
999px

Console