分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

HTML JS 数据校验

发布时间:2023-09-06 01:21责任编辑:白小东关键词:HTML

用到了html字符串校验,这里记录一下。

 1 <html> 2 ????<head> 3 ????????<script type="text/javascript"> 4 ????????????function on_click(){ 5 ????????????????var email = document.getElementById("input1").value.trim(); 6 ????????????????var telph = document.getElementById("input2").value.trim(); 7 ?????????8 ????????????????if(email == "" ||telph == ""){ 9 ????????????????????alert("The email or telph is blank!!!");10 ????????????????????return false;11 ????????????????}12 ????????????????13 ????????????????if (email.indexOf("@") == -1){14 ????????????????????alert("an @ is not in an email !!!");15 ????????????????????return false;16 ????????????????}17 ????????????????18 ????????????????var re = /^([0-9]{3}-)?[0-9]{3}-[0-9]{4}$/;19 ????????????????if (re.test(telph) == false){20 ????????????????????alert("telph number is not match xxx-xxxx or xxx-xxx-xxxx")21 ????????????????????return false;22 ????????????????}23 ????????????????24 ????????????????alert("ok, email:" + email + ", telephone:" + telph);25 ????????????????return true;26 ????????????}27 ????????</script>28 ????</head>29 ????30 ????<body>31 ????????email: <input id = "input1"> </input> <br> ???<!--必须有@-->32 ????????telph: <input id = "input2"> </input> <br> ???<!--必须满足xxx-xxxx or xxx-xxx-xxxx-->33 ????????<button type="button" onclick=on_click()>test</button>34 ????</body>35 </html>

如图:

校验两个字段均不能为空

校验email字段必须包含@

校验telph字段必须满足正则表达式

HTML JS 数据校验

原文地址:http://www.cnblogs.com/xudong-bupt/p/7750655.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved