一个判断字符的问题```
在JSP中怎么判断是否是合法字符啊???就是只存在a-z和0-9所组成的字符
<%!
String aa= "123 ";
String bb= "abc ";
%>
<%!
public String check(String fstrData)
{
String S =fstrData;
if(S= "[^a-zA-Z0-9_]+) ")
{
return true;
}else{
return false;
}
}
%>
<%
out.println(check(bb));
%>
我这样写报了
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 14 in the jsp file: /shiyan.jsp
Generated servlet error:
Type mismatch: cannot convert from String to boolean
An error occurred at line: 14 in the jsp file: /shiyan.jsp
Generated servlet error:
Type mismatch: cannot convert from boolean to String
An error occurred at line: 14 in the jsp file: /shiyan.jsp
Generated servlet error:
Type mismatch: cannot convert from boolean to String
这样的错误
------解决方案--------------------if(S= "[^a-zA-Z0-9_]+) ")
{
这个错了,你是想用正则的话怎么也得有pattern啊,再说了=也是赋值的操作
------解决方案--------------------改成下面的方法试试,记得要把java.util.regex.Pattern包引进来
<%!
String aa= "123 ";
String bb= "abc ";
%>
<%!
public String check(String fstrData)
{
String S =fstrData;
boolean b = Pattern.matches( "[^a-zA-Z0-9_] ", "S ");
if(b)
{
return true;
}else{
return false;
}
}
%>
<%
out.println(check(bb));
%>
------解决方案--------------------不用正则也可以判断,你可以参照我下面的一段代码:
function isNumber(str){
for(var i=0;i <str.length;i++){
var onechar=str.substring(i,i+1)
if(onechar < "0 " || onechar> "9 "){
alert( "Please input number! ")
return false
}
}
}
------解决方案-------------------- <%!
String aa= "123 ";
String bb= "abc ";
public boolean check(String fstrData)
{