当前位置:网站首页>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
边栏推荐
- APP automation test framework - UiAutomator2 introductory
- 什么是源文件?
- Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
- How to Make Your Company Content GDPR Compliant
- LeetCode26:删除有序数组中的重复项
- AI识万物:从0搭建和部署手语识别系统
- Use zeros(), ones(), fill() methods to generate data in TF
- Tensorflow中使用convert_to_tensor去指定数据的类型
- Photometric Stereo 光度立体法三维重建
- The round functions in the np, ceil function and floor function
猜你喜欢
APP automation test framework - UiAutomator2 introductory
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
[corctf 2022] section
ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
How to Make Your Company Content GDPR Compliant
Word箭头上面怎么打字
Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool
SQL语句及索引的优化
Problems with compiling SIP with QGIS
随机推荐
Solution: Edu Codeforces 109 (div2)
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
MySQL:错误1153(08S01):得到的数据包大于“ max_allowed_packet”字节
PHP 二维数组根据某个字段排序
Use zeros(), ones(), fill() methods to generate data in TF
Excel如何打出正负号?Excel打出正负号的方法
STC8H开发(十五): GPIO驱动Ci24R1无线模块
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
Word文档怎么输入无穷大符号∞
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
[Generic Programming] Full Detailed Explanation of Templates
AI识万物:从0搭建和部署手语识别系统
场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
Tensorflow中使用convert_to_tensor去指定数据的类型
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
“稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
Word第一页空白页怎么删除?删除Word第一页空白页方法教程
4D Summary: 38 Knowledge Points of Distributed Systems
消防安全培训|“蓝朋友”,开课了!
leetcode:数组中的第K个最大元素