使用 javascript 在 JSP 中查找空白字符

Finding a whitespace character in JSP using javascript

我需要一些帮助来实现 JSP 输入字段中的空白字符检查。

这是我的代码和现有 JSP 代码的样子:

function validation(){
var wsregex = /\s/g;
var input = document.getElementById("Division_").value;
var result;

if (result = input.match(wsregex)){
    alert ("There is a whitespace in the Name field");
    return false;
 }

JSP代码:

<input type="hidden" name="Division_<%= divisionSeq %>" value="exist" />
<input class="button" type="submit" name="submit" value="Submit" onclick="return validate(this.form, <%=divisionSeq%>)"

除了我拥有的,我不知道还能改变什么。 JSP 代码是由一位顾问构建的,他没有留下任何文档。

谢谢, E.

我不确定您调用 javascript 函数的方式。 我正在分享我为让它发挥作用所做的工作。

<html>
<head>
<script>
function validation(){
 var wsregex = /\s/g;
 var input = document.getElementById("input").value;
 var result;

 if (result = input.match(wsregex)){
     alert ("There is a whitespace in the Name field");
     return false;
  }
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Enter a text : <input id="input" type="text" /><br>
<input type="submit"
value="Submit" onclick="validation()" />
</body>
</html>

只需输入任何文本,submit.It 将检查空格并在找到时发出警报。

Javascript代码

function validation(){
var wsregex = /\s/g;
var input = document.getElementById("Division_1").value;
var result;

if (result = input.match(wsregex)){
    console.log ("There is a whitespace in the Name field");
    return false;
 }

JSP代码:

input type="hidden" name="Division_<%= divisionSeq %>" value="exist" />
<input class="button" type="submit" name="submit" value="Submit" onclick="return validate()"

<%=divisionSeq%> scriptlet 是一个计数器,所以我欺骗了 js 函数来预测初始值。