zoukankan      html  css  js  c++  java
  • JS生成数字加减乘法验证码

    给大家分享一个简单的js验证码生成代码

    PS:该代码依赖Jquery1.4版本以上

    • 传入元素 如productionVerificationCode(#(("a")) 反回验证码的结果,#)("a")元素写入验证码
    
    //----[生成数字加减乘法验证码](传入写入元素,返回验证码计算结果)
    function productionVerificationCode(element) {
    	var code = 9999;
    	var ranColor = '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6); //随机生成颜色
    	// alert(ranColor)
    	var ranColor2 = '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);
    	var num1 = Math.floor(Math.random() * 100);
    	var num2 = Math.floor(Math.random() * 100);
    	//随机算法
    	var tmparith = Math.floor(Math.random() * 3);
    	var $html = "";
    	switch(tmparith) {
    		case 1:
    			code = num1 + num2;
    			$html = num1 + ' + ' + num2 + ' = ?';
    			break;
    		case 2:
    			if(parseInt(num1) < parseInt(num2)) {
    				var tmpnum = num1;
    				num1 = num2;
    				num2 = tmpnum;
    			}
    			code = num1 - num2;
    			$html = num1 + ' - ' + num2 + ' = ?';
    			break;
    		default:
    			code = num1 * num2;
    			$html = num1 + ' × ' + num2 + ' = ?';
    			break;
    	}
    	element.val($html); //写入验证码
    	if(element.hasClass("nocode")) {
    		element.removeClass("nocode");
    		element.addClass("code");
    	}
    	element.css('background', ranColor);
    	element.css('color', ranColor2);
    	return code;
    }
    //----[END][生成数字加减乘法验证码]
    
    
    
  • 相关阅读:
    MVC与MVVM
    js正则删除字符串中的部分内容(支持变量和特殊符号)
    小程序之rpx适配方案
    表单元素内容禁用拼写检查
    vue组件实例的生命周期
    Windows下生成目录结构树命令
    DRF之解析器源码解析
    restful规范快速记忆
    python报错之OSError
    xlrd、xlwt
  • 原文地址:https://www.cnblogs.com/userzf/p/9808735.html
Copyright © 2011-2022 走看看