当前位置:网站首页>JS–比想象中简单
JS–比想象中简单
2022-08-09 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
刚看完JavaScript视频的时候,感觉一点印象都没有,而且大部分效果自己都做不出来。现在看了一点SSH的视频,发现JS其实跟我们其他语言编写的代码很相似,先从最简单的开始,不断地积累就学会了。
比如说登录界面验证表单是否为空
首先在表单中添加onsubmit方法
<span style="font-family:KaiTi_GB2312;font-size:18px;"><form id="registerForm" οnsubmit=" checkForm();">
</form></span>
对应的在JS中编写相应的checkForm()方法
<span style="font-family:KaiTi_GB2312;font-size:18px;"><script> //校验表单内容是否为空
function checkForm(){
//校验用户名
//获得用户名文本框的值
var username=document.getElementById("username").value;
if(username == null || username == ''){
alert("用户名不能为空!");
return false;
}
//校验密码
//获得密码框的值
var password = document.getElementById("password").value;
if(password == null || password ==''){
alert("密码不能为空!");
return false;
}
}
</script></span>
从上面一段代码可以看出,跟我们平时写的VB,C#多么相似,只是在获得文本框的值的时候是通过DOM方法来取的,document.getElementById(“username”).value,当时就是因为好多类似于document.getElementById()这样的东西,把自己整蒙了,所以现在还是要一点一点还回来的。
上面是简单的表单是否为空的验证,下面就是验证邮箱格式是否正确的方法,其实也很简单,运用正则表达式就可以了
<span style="font-family:KaiTi_GB2312;font-size:18px;"> //校验邮箱
var email = document.getElementById("email").value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(filter.test(email)){
return true;
}else{
alert("邮箱格式不正确!");
return false;
}</span>
这样的JavaScript代码还是很简单的吧,先从简单的开始,这样就能相信自己能学会。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105730.html原文链接:https://javaforall.cn
边栏推荐
- SecureCRT强制卸载
- Problems with compiling SIP with QGIS
- 宝塔实测-搭建LightPicture开源图床系统
- Word第一页空白页怎么删除?删除Word第一页空白页方法教程
- 小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
- Deceptive Dice
- [Cloud Native] 4.2 DevOps Lectures
- 【stack】【queue】【priority_queue】【deque】详解
- Several ways to draw timeline diagrams
- Technology Sharing | How to use the JSON Schema mode of interface automation testing?
猜你喜欢
Word箭头上面怎么打字
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
Simple questions peek into mathematics
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
Wps下划线怎么弄?Wps添加下划线的最全方法
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
4D Summary: 38 Knowledge Points of Distributed Systems
Excel如何打出正负号?Excel打出正负号的方法
随机推荐
Tensorflow中使用convert_to_tensor去指定数据的类型
Word文档怎么输入无穷大符号∞
TF generates uniformly distributed tensor
4D Summary: 38 Knowledge Points of Distributed Systems
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
mysql multi-table left link query
SecureCRT背景配色
kvm虚拟机出现启动不了,NOT available,PV大于分区
TF uses constant to generate data
2.1.5 大纲显示问题
SecureCRT sets the timeout period for automatic disconnection
SecureCRT 设置超时自动断开连接时长
Wps下划线怎么弄?Wps添加下划线的最全方法
Sudoku | Backtrack-7
The round functions in the np, ceil function and floor function
字符串哈希(2014 SERC J题)
【云原生】4.2 DevOps 精讲篇
QGIS编译SIP的问题
hdu 1333 Smith Numbers(暴力思路)
mysql多表左链接查询