function checkType (str, type) {
switch (type) {
// 验证邮箱
case 'email':
return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
// 验证手机号
case 'phone':
return /^1[3|4|5|7|8][0-9]{9}$/.test(str);
// 验证座机号
case 'tel':
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
// 验证邮箱
case 'number':
return /^[0-9]$/.test(str);
// 验证英文字母
case 'english':
return /^[a-zA-Z]+$/.test(str);
// 验证中文字
case 'chinese':
return /^[\u4E00-\u9FA5]+$/.test(str);
// 验证小写
case 'lower':
return /^[a-z]+$/.test(str);
// 验证大写
case 'upper':
return /^[A-Z]+$/.test(str);
default :
return true;
}
}
console.log(checkType ('hjkhjhT','lower')) //false
常用校验验证:邮箱,手机号,名字,大写,小写
最新推荐文章于 2025-11-14 17:39:33 发布
本文介绍了一个使用JavaScript实现的函数,该函数通过正则表达式来检查输入的字符串是否符合特定类型,包括电子邮件、电话号码、数字、英文字符、中文字符等。这个函数可以用于前端表单验证,确保用户输入的数据格式正确。

1479

被折叠的 条评论
为什么被折叠?



