一个判断字符的问题```
在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)
{
boolean flag = false ;
String S =fstrData;
char s[] = S.toCharArray();
for(int i=0;i <s.length;i++){
if(s[i]> = 'a '&&s[i] <= 'z '||s[i]> = '0 '&&s[i] <= '9 '){
System.out.println( "success ");
flag = true;
}
else{
System.out.println( "false ");
}
}
return flag;
}
%>
<%
out.println(check(aa));
%>
[解决办法]
兄弟们
<%!
String aa= "123 ";
String bb= "liu6787494 ";
%>
<%!
public boolean check(String fstrData)
{
String S =fstrData;
boolean b = Pattern.matches( "[a-zA-Z0-9] ",S);
return b;
}
%>
<%
out.println(check(bb));
out.println(bb);
%>
我这样写不管怎么改bb的值都是显示false这是为什么`
[解决办法]
out.println( "sadf123 ".matches( "[a-zA-Z0-9]+ "));
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html