产生随机4个英文字母详情看下面代码!
<!doctype
html>
<html
lang="en">
<head>
<meta
charset="UTF-8">
</head>
<div
id="result"></div><button
onclick="getRanNum()">生成</button>
<body>
<script>
var
result
=
[]
function
getRanNum(){
result
=
[]
for(var
i=0i<4i++){
var
ranNum
=
Math.ceil(Math.random()
*
25)
//生成一个0到25的数字
//大写字母'A'的ASCII是65,A~Z的ASCII码就是65
+
0~25然后调用String.fromCharCode()传入ASCII值返回相应的字符并push进数组里
result.push(String.fromCharCode(65+ranNum))
}
document.getElementById('result').innerText
=
result.toString()
}
</script>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html charset=utf-8"><button onClick="return rn()">随机生成名字</button>
<span id="target"></span>
<script>
function rn(){
//这个名字库只能自己填充内容了,js没这么智能,可以自己拼个名字出来...
var store = ['Marry','Jhon','Tom','Lily'],
el = document.getElementById('target')
return el.innerHTML = store[Math.floor(Math.random()*store.length)]
}
</script>
首先去掉首末位空格。\x0d\x0avarstr=input.replace(/(^\s*)|(\s*$)/g,"")\x0d\x0a验证名字\x0d\x0a字符头尾是字母,中间由空格和字母组成,中间可以有多个空格,如:TaylorGreSwift\x0d\x0a/^[A-Za-z][A-Za-z\s]*[A-Za-z]$/.test(str)\x0d\x0a如果想将中间无论多少空格替换成一个,可以再加这个:\x0d\x0a\x0d\x0astr=str.replace(/\s+/g,"")\x0d\x0a\x0d\x0a字符头尾是字母,中间由空格和字母组成,中间只有一个空格,如:TaylorGreSwift\x0d\x0a\x0d\x0a/^([A-Za-z]+\s?)*[A-Za-z]$/.test(str)