首页 > 编程语言 >JavaScript函数--"check"

JavaScript函数--"check"

时间:2022-11-15 23:56:43浏览次数:36  
标签:return -- JavaScript value getElementById var false document check

JS中一个较常见的函数"checkForm"。是用来检验表单信息的正确性。

步骤如下:

1:表单<form>添加提交事件

<form action="#" method="get" name="regForm" οnsubmit="return checkForm()">

</form>

添加οnsubmit="return checkForm()" 其中红色函数名字随意。

2:在表中<td>添加id字段

<td>用户名</td>
<td>
<input type="text" name="user" id="user"/>
</td>

3 : 编写check()函数,进行校验

<scrtipt>

//1用户名不能为空

var uValue = document.getElementById("user").value;

if(uValue==""){

alter("用户名不能为空");

return false;

}

</scrtipt>

其中绿色字段与<td>中必须相同。

简单的案例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<script>
/*检查表单函数*/
function checkForm(){
//alert("aa");
//1 获取用户输入的数据
var uValue = document.getElementById("user").value;
if(uValue==""){
//2给出错误信息
alert("用户名不能为空!");
return false;
}

/*校验密码*/
var pValue = document.getElementById("password").value;
if(pValue==""){
alert("密码不能为空!");//
return false;
}

/*校验确认密码*/
var rpValue = document.getElementById("repassword").value;
if(rpValue!=pValue){
alert("两次密码输入不一致!");
return false;
}

/*校验邮箱地址*/
var eValue = document.getElementById("email").value;
if(!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(eValue)){
alert("邮箱地址不对!");
return false;
}
}
</script>
</head>
<body>
<table>
<!-- 1 logo部分 -->
<tr>
<td>
<table>
<tr>
<td>
StevenLu SHOP
</td>
<td>
<a href="#">登入</a>
<a href="#">注册</a>
<a href="#">购物车</a>
</td>
</tr>
</table>
</td>
</tr>
<!-- 2 导航部分 -->
<tr>
<td >
<a herf="#">
<font size="5" color="whirte">首页</font>
</a>
<a herf="#">
<font size="5" color="whirte">手机数码</font>
</a>
<a herf="#">
<font size="5" color="whirte">电脑办公</font>
</a>
<a herf="#">
<font size="5" color="whirte">家用电器</font>
</a>
</td>
</tr>
<!-- 3 注册列表 -->
<tr>
<form action="#" method="get" name="regForm" οnsubmit="return checkForm()">
<table>
<tr height="40px">
<td colspan="2">
<font size="4">会员注册</font>
</td>
</tr>
<tr>
<td>用户名</td>
<td>
<input type="text" name="user" id="user"/>
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" name="password" id="password"/>
</td>
</tr>
<tr>
<td>确认密码</td>
<td>
<input type="repassword" name="password" id="repassword"/>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text"/ name="email" id="email">
</td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女"/>女
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="注册"/>
</td>
</tr>
</table>
</form>
</tr>
</table>
<!-- 版权 -->
<td align="center">
<p>Copyright @2018.6.27 StevenLu</p>
</td>
</body>

</html>

标签:return,--,JavaScript,value,getElementById,var,false,document,check
From: https://www.cnblogs.com/yitongtianxia666/p/16894488.html

相关文章

  • 13条设计原则
                                         ......
  • 读图片和标记图片
    importosimportcv2importpandasaspdfiles='images'label='labels'importnumpyasnpfiles1=os.listdir(files)forfileinfiles1:  photo=files+'/'+f......
  • python 中 循环结构同时传入多个参数
     001、>>>list1=[("aa",100,400),("bb","kk","yy"),(33,400,500)]>>>fori,j,kinlist1:##利用列表、元组同时传入多个参数...print(i,......
  • 强化学习代码实战-07 Actor-Critic 算法
    Actor(策略网络)和Critic(价值网络)Actor要做的是与环境交互,并在Critic价值函数的指导下用策略梯度学习一个更好的策略。Critic要做的是通过Actor与环境交互收集的数......
  • ElasticSearch中的基本概念
    1、关系型数据库VSelasticsearch2、索引(Index)一个索引就是一个拥有几分相似特征的文档的集合。比如说,可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据......
  • SpringBoot 11: Spring + SpringMVC + SpringBoot常用注解总结
    创建对象的:@Controller: 放在类的上面,创建控制器对象,注入到容器中@RestController: 放在类的上面,创建控制器对象,注入到容器中作用:是@Controller@ResponseBody......
  • SpringBoot 08: SpringBoot综合使用 MyBatis, Dubbo, Redis
    业务背景Student表CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENT,`name`varchar(255)COLLATEutf8_binDEFAULTNULL,`phone`varchar(11)......
  • 0_0
    你答应过自己的你说无论发生了什么都要全力以赴你决定过哪怕要为此付出很多代价(甚至因此和miaowu吵过架你的周末,和小伙伴去唱歌的快乐,寒暑假甚至是去年运动会约好了和......
  • 2022 年杭电多校第六场 Multiply 2 Divide 2
    2022年杭电多校第六场Multiply2Divide2题意:BXY的序列\(a\)长度为\(n\)\((1\leqn\leq10^5,1\leqa_i\leq10^5)\)对于每个操作,他选择一个数字\(a_i(1\leqi\leqn......
  • windows--cmake与c++的使用教程(6)
    1概述本文基于前文环境本节目标:了解CMAKE内置的一些变量以及对应的值2CMake内置变量前面的文章中已经出现了CMAKE_CURRENT_SOURCE_DIR,当时没有说明,现在补上,CM......