之前有一次,因为微信支付秘钥长度问题,一直导致支付对接不上,最后专门自己写了一个字符串长度检测及生成的,供大家参考下,做了防止f12进入开发调试及复制操作,需要的直接复制即可。
<!DOCTYPE html>
<html>
<head>
<title>字符串位数检测及随机数生成</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<style type="text/css">
*{
margin:0;
padding: 0;
border: 0;
}
body,html{
max-width: 750px;
margin: 5rem auto 0;
}
.txt,#sub,#sub1{
display:block;
float: left;
height: 50px;
line-height: 50px;
border: 1px solid red;
box-sizing: border-box;
margin-bottom: 15px;
}
.txt{
width: 75%;
margin-left: 2.5%;
padding-left:5%;
border-right:none;
border-bottom-left-radius:20px;
border-top-left-radius: 20px;
}
#sub,#sub1{
width: 20%;
text-align: center;
border-bottom-right-radius:20px;
border-top-right-radius: 20px;
background-color: red;
color: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<input placeholder="请输入字符串并点确定" class="txt" id="txt"/>
<span id="sub" onclick="sub()">确定</span>
<input placeholder="点击生成将生成32位随机数" class="txt" id="txt1" disabled="disabled"/>
<span id="sub1" onclick="sub1(32)">生成</span>
</body>
<script type="text/javascript">
var txt = document.getElementById('txt');
function sub(){
if(!txt.value.length){
alert('请输入字符串');
}else{
alert(txt.value.length);
}
};
function sub1(n) {
var chars = ['0','1','2','3','4','5','6','7','8','9',
'a','B','b','c','E','f','G','H','i','J','k','l','M',
'N','O','P','q','R','s','T','u','V','W','x','Y','z'];
var res = "";
for(var i = 0; i < n ; i++) {
var id = Math.floor(Math.random()*36);
res += chars[id];
}
document.getElementById('txt1').value=res;
}
//江湖中人2022年5月5日22:13:51 屏蔽右键-不包含IE浏览器
document.oncontextmenu = function (event){
if(window.event){
event = window.event;
}
try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}
catch (e){
return false;
}
};
// 江湖中人2022年5月5日22:13:42屏蔽f12-不包含IE浏览器
window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
if (event.keyCode == 123) {
event.preventDefault(); // 阻止默认事件行为
window.event.returnValue = false;
}
}
</script>
</html>