11.6
// 前端验证示例(项目编号)
function checkProjectId(id) {
const reg = /^\d{8}$/; // 8位数字
const yearMonth = id.substr(0,4); // 前4位
return reg.test(id) && yearMonth >= "2410" && yearMonth <= "9912";
}在IDEA哪里填入
在 IDEA 中,这段前端验证代码(JavaScript)应该放在项目的 前端资源目录 中,具体位置取决于你的项目结构设计:
推荐位置:单独的 JS 文件中
在项目的 WebContent 目录下,新建 js 文件夹(如果没有):路径示例:WebContent/js/validate.js
将这段验证代码写入 validate.js 文件中:
javascript
运行
// WebContent/js/validate.js
function checkProjectId(id) {
const reg = /^\d{8}$/; // 验证8位数字
const yearMonth = id.substr(0,4); // 提取前4位(年月)
// 验证格式:8位数字 + 年月合理性(示例中2410代表2024年10月,可根据实际需求调整范围)
return reg.test(id) && yearMonth >= "2410" && yearMonth <= "9912";
}
在需要验证的 JSP 页面(如项目立项页面 addProject.jsp)中引入该 JS 文件:
jsp
alternative:直接嵌入 JSP 页面
如果验证逻辑仅在某个页面使用,也可以直接在该 JSP 的

浙公网安备 33010602011771号