日期:2014-05-16  浏览次数:20298 次

js代码小结
01.不能为空

02.<input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')">

03.

04.只能输入英文和数字

05.<input onblur="if(/[^0-9a-zA-Z]/g.test(value))alert('有错')">

06.<input onkeyup="value=value.replace(/[^0-9a-zA-Z]/g,'')"/>

07.<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9]/g,'')">

08.

09.判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母

10./^([a-zA-z_]{1})([\w]*)$/g.test(str)

11.

12.只能输入数字

13.<input name="text" type="text" id="NewPage" onKeyUp="value=value.replace(/\D/g,'')" onafterpaste="value=value.replace(/\D/g,'')" >

14.

15.只能输入中文

16.<input type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">

17.

18.只能输入英文

19.<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')">

20.<input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">

21.

22.只能输入中文、英文、数字、@符号和.符号

23.<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')">

24.

25.只允许输入英文,且不能粘贴也无法弹出粘贴菜单

26.<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false"/>

27.

28.只能输入数字和点号(注意:在[^\d\.]里的d不能写成大写D,否则就变成除了数字以外的所有字符)

29.<input name="price" type="text" size="8" maxlength="8" onkeyup="value=value.replace(/[^\d\.]/g,'')" >

30.

31.总而言之:先在<input>里输入onkeyup="value=value.replace(/[^\X]/g,'')" 然后在(/[\X]/g,'')里的X换成你想输入的代码就可以了

32.

33.中文:u4E00-u9FA5

34.数字:d、0-9

35.英文:a-z、A-Z

36.其它符号@,点或其它符号.也可以多个,用\隔开就行了.

37.例如:

38.中、英文和数字加@符号加点符号:\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.

39.

40.若想在文本框里不能右键弹出菜单和不能粘贴进复制的信息的话就要在<input>里输入 onKeyDown="fncKeyStop(event)" onpaste="return false" oncontextmenu="return false;"
复制代码

---------------------
清空表单的js
1.  function reset(){
           var form = document.getElementById("currform");
           form.reset();
         }
2.document.getElementById("orgName").value="";

3.删除
function delete1(){
var form = document.getElementById("currform");
form.action="org_delete";
form.submit();
}
此处要是用delete则不行。为定义有其他含义代码。

4.checkbox传值--注意没有Int
function delete1(){
  var form = document.getElementById("currform");
  var checks = document.getElementsByName("orgOid");
 
for(i=0;i<checks.length;i++){     if(checks[i].checked){
    var element = checks[i].value;
    form.action="org_delete?orgOid="+element;
    form.submit();
    }
  }
}

5.出现confirm框
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
  {
  alert("You pressed OK!");
  }
else
  {
  alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>


6.throw的一个验证
var x=prompt("请输入 0 至 10 之间的数:","")
try
{
if(x>