Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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="http://xxx.com/register" id="registerForm" method="post">
  
 请输入用户名:
  <input type="text" name="userName" />
  
 请输入密码:
  <input type="text" name="password" />
  
 请输入手机号码:
  <input type="text" name="phoneNumber" />
  <button>
    提交
  </button>
</form>
              
            
!

CSS

              
                
              
            
!

JS

              
                // 例子1
const strategy = {
  S: function (salary) {
    return salary * 4;
  },
  A: function (salary) {
    return salary * 3;
  },
  B: function (salary) {
    return salary * 2;
  }
};

const calculateBonus = function (level, salary) {
  // document.write( strategy[level](salary) );
  return strategy[level](salary);
};

calculateBonus("A", 1000);

// 例子2

// 表单dom
const registerForm = document.getElementById("registerForm");
// 表单规则
const rules = {
  userName: [
    {
      strategy: "isNonEmpty",
      errorMsg: "用户名不能为空"
    },
    {
      strategy: "minLength:10",
      errorMsg: "用户名长度不能小于10位"
    }
  ],
  password: [
    {
      strategy: "minLength:6",
      errorMsg: "密码长度不能小于6位"
    }
  ],
  phoneNumber: [
    {
      strategy: "isMobile",
      errorMsg: "手机号码格式不正确"
    }
  ]
};

// 策略类
var strategies = {
  isNonEmpty: function (value, errorMsg) {
    if (value === "") {
      return errorMsg;
    }
  },
  minLength: function (value, errorMsg, length) {
    console.log('length',length);
    if (value.length < length) {
      return errorMsg;
    }
  },
  isMobile: function (value, errorMsg) {
    if (!/(^1[3|5|8][0-9]{9}$)/.test(value)) {
      return errorMsg;
    }
  }
};

// 验证类
const Validator = function () {
  this.cache = [];
};

// 添加验证方法
Validator.prototype.add = function ({ dom, rules }) {
  rules.forEach((rule) => {
    const { strategy, errorMsg } = rule;
    console.log('rule',rule);
    const [strategyName, strategyCondition] = strategy.split(":");
    console.log('strategyName',strategyName);
    const { value } = dom;
    this.cache.push(
      strategies[strategyName].bind(dom, value, errorMsg, strategyCondition)
    );
  });
};

// 开始验证
Validator.prototype.start = function () {
  let errorMsg;
  this.cache.some((cacheItem) => {
    const _errorMsg = cacheItem();
    if (_errorMsg) {
      errorMsg = _errorMsg;
      return true;
    } else {
      return false;
    }
  });

  return errorMsg;
};

// 验证函数
const validatorFn = () => {
  const validator = new Validator();
  console.log('validator.add',validator.add);

  Object.keys(rules).forEach((key) => {
    console.log('rules[key]', rules[key]);
    validator.add({
      dom: registerForm[key],
      rules: rules[key]
    });
  });

  const errorMsg = validator.start();
  return errorMsg;
};

// 表单提交
registerForm.onsubmit = () => {
  // debugger;
  const errorMsg = validatorFn();
  if (errorMsg) {
    alert(errorMsg);
    return false;
  }
  return false;
};

              
            
!
999px

Console