怎样限制input内输入单引号"'"? <script language="JavaScript" type="text/JavaScript">function CheckNum(){ if (window.event.keyCode==39) { window.event.keyCode=0; }}</script> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <input name="abc" type="text" size="4" onKeyPress="CheckNum()"> TO JK_10000(JK):我的页面中有一个输入框<input type=text name=title size=46 maxlength=50 value=<%=title%>>我的目的是将数据库中的title字段的内容获取出来显示在输入框内,如果按照上面value的写法,那么如果title内容之中有空格,比如title="a b c",那么生成的页面value=a,不合要求;如果写成value='<%=title%>',那么如果用户输入的title中有引号,则会在第一个引号处截断,也不合要求,请教高手有何方法能够解决。谢谢 如果是jsp:数据输出时需要经过转化,例如显示为input value=""的内容,可以经过以下类里htmlEncoder的方法转化。<input type=text name=title size=46 maxlength=50 value="<%=title%>" >--->>><input type=text name=title size=46 maxlength=50 value="<%=CommStr.htmlEncoder(title)%>" > /* * Created by Seamus * Updated on 2004-10-21 by JK * */package com.jk.util;public class CommStr{ /** *replace the old string to new string in the given destination string. */ public static java.lang.String strReplace(java.lang.String destStr, java.lang.String oldStr, java.lang.String newStr) { if(destStr==null) return ""; String tmpStr = destStr; int foundPos = tmpStr.indexOf(oldStr); while (foundPos>=0) { tmpStr = tmpStr.substring(0,foundPos) + newStr + tmpStr.substring(foundPos + oldStr.length(),tmpStr.length()); foundPos = tmpStr.indexOf(oldStr,foundPos+newStr.length()); } return tmpStr; } /** *Encode for HTML. */ public static String htmlEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"<","<"); res_str=strReplace(str,">","&rt;"); res_str=strReplace(str,"\"","""); res_str=strReplace(str,"'","'"); return res_str; } /** *Encode for HTML-Text. */ public static String htmlTextEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"<","<"); res_str=strReplace(str,">","&rt;"); res_str=strReplace(str,"\"","""); res_str=strReplace(str,"'","'"); res_str=strReplace(str," "," "); res_str=strReplace(str,"\r\n","<br>"); res_str=strReplace(str,"\r","<br>"); res_str=strReplace(str,"\n","<br>"); return res_str; } /** *Encode for URL. */ public static String urlEncoder(String str) { return java.net.URLEncoder.encode(str) ; } /** *Encode for XML. */ public static String xmlEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"&","&"); res_str=strReplace(res_str,"<","<"); res_str=strReplace(res_str,">",">"); res_str=strReplace(res_str,"\"", """); res_str=strReplace(res_str,"\'", "´"); return res_str; } /** *Encode for SQL. */ public static String sqlEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"'","''"); return res_str; } /** *Encode for Javascript. */ public static String jsEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"\\","\\\\"); res_str=strReplace(str,"'","\\'"); res_str=strReplace(str,"\"","\\\""); res_str=strReplace(str,"\r\n","\\\n"); res_str=strReplace(str,"\n","\\\n"); res_str=strReplace(str,"\r","\\\n"); return res_str; }} 正则表达式 怎么在html中拿到值 初学开发jquery插件的小问题 DIV的 onmouseOver |zyciis| 想保存某个网站的网页 但是点击保存时,出现一行字,"该网页可能无法正确保存,是否保存"点击保存后,该网页的内容无法正确查看 我要做个东西,大家帮我看下!! 如果做到点击页面的一个链接,在页面的iframe里显示链接的页面 请教:如何实现文件拖放? 在网页中插入的flash怎样能播放一段时间自动关闭,就像以前csdn主页上的效果??? 非常简单的问题? 表单提交问题,在线等待!!! 无聊,散分散代码... 急,二进制数据问题
我的页面中有一个输入框<input type=text name=title size=46 maxlength=50 value=<%=title%>>我的目的是将数据库中的title字段的内容获取出来显示在输入框内,如果按照上面value的写法,那么如果title内容之中有空格,比如title="a b c",那么生成的页面value=a,不合要求;
如果写成value='<%=title%>',那么如果用户输入的title中有引号,则会在第一个引号处截断,也不合要求,请教高手有何方法能够解决。谢谢
<input type=text name=title size=46 maxlength=50 value="<%=title%>" >
--->>>
<input type=text name=title size=46 maxlength=50 value="<%=CommStr.htmlEncoder(title)%>" >
/*
* Created by Seamus
* Updated on 2004-10-21 by JK
*
*/
package com.jk.util;public class CommStr
{
/**
*replace the old string to new string in the given destination string.
*/
public static java.lang.String strReplace(java.lang.String destStr, java.lang.String oldStr, java.lang.String newStr)
{
if(destStr==null)
return "";
String tmpStr = destStr;
int foundPos = tmpStr.indexOf(oldStr);
while (foundPos>=0)
{
tmpStr = tmpStr.substring(0,foundPos) + newStr + tmpStr.substring(foundPos + oldStr.length(),tmpStr.length());
foundPos = tmpStr.indexOf(oldStr,foundPos+newStr.length());
}
return tmpStr;
} /**
*Encode for HTML.
*/
public static String htmlEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"<","<");
res_str=strReplace(str,">","&rt;");
res_str=strReplace(str,"\"",""");
res_str=strReplace(str,"'","'");
return res_str;
} /**
*Encode for HTML-Text.
*/
public static String htmlTextEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"<","<");
res_str=strReplace(str,">","&rt;");
res_str=strReplace(str,"\"",""");
res_str=strReplace(str,"'","'");
res_str=strReplace(str," "," ");
res_str=strReplace(str,"\r\n","<br>");
res_str=strReplace(str,"\r","<br>");
res_str=strReplace(str,"\n","<br>");
return res_str;
} /**
*Encode for URL.
*/
public static String urlEncoder(String str) {
return java.net.URLEncoder.encode(str) ;
} /**
*Encode for XML.
*/
public static String xmlEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"&","&");
res_str=strReplace(res_str,"<","<");
res_str=strReplace(res_str,">",">");
res_str=strReplace(res_str,"\"", """);
res_str=strReplace(res_str,"\'", "´");
return res_str;
} /**
*Encode for SQL.
*/
public static String sqlEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"'","''");
return res_str;
} /**
*Encode for Javascript.
*/
public static String jsEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"\\","\\\\");
res_str=strReplace(str,"'","\\'");
res_str=strReplace(str,"\"","\\\"");
res_str=strReplace(str,"\r\n","\\\n");
res_str=strReplace(str,"\n","\\\n");
res_str=strReplace(str,"\r","\\\n");
return res_str;
}}