初次到这里,请教个菜问,顺便放点分 for(var i=0;i<document.form1.text1.value.length;i++){if(document.form1.text1.value.substr(i,1)==" ") alert("有空格")} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sorry.我想说的是去掉头尾的空格后不能为空.象VBS里的,trim(str).. <input name=show><input type=button value=检测 onclick= 'if(document.all("show").value.indexOf(" ") >=0) alert("有空格");else alert("OK.")' > <script language=javascript>String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}String.prototype.LTrim = function(){return this.replace(/(^\s*)/g, "");}String.prototype.RTrim = function(){return this.replace(/(\s*$)/g, "");}String.prototype.getLength = function(){ var l=this.length; var n=l for (var i=0;i<l;i++){ if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++ } return n}sStr=new String("")alert("["+sStr+"]")alert("["+sStr.LTrim()+"]")alert("["+sStr.RTrim()+"]")alert("["+sStr.Trim()+"]")alert(sStr.RealLength());</script> <script language=javascript>String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}String.prototype.LTrim = function(){return this.replace(/(^\s*)/g, "");}String.prototype.RTrim = function(){return this.replace(/(\s*$)/g, "");}String.prototype.getLength = function(){ var l=this.length; var n=l for (var i=0;i<l;i++){ if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++ } return n}sStr=new String("")alert("["+sStr+"]")alert("["+sStr.LTrim()+"]")alert("["+sStr.RTrim()+"]")alert("["+sStr.Trim()+"]")alert(sStr.RealLength());</script> 嗯,是个方法,然后我再用lastIndexOf确定后面有没有空格?只是还是没有trim来的清晰,不好意思我这样说.也许我只能自己写个function,先,indexof,再lastindexof,再substr.....我是想把前后的空格都去掉.请大家多帮忙. try it please:/** * 去掉字符串左/右端空格 * @param :str 字符串;flag 控制标志:0 去掉两端空格,缺省值、1 去掉左端空格、2 去掉右端空格 * @return :去掉前后空格的String对象。 */function trim(str,flag) { if (!flag) flag = 0; if (flag!=2) while (str.indexOf(' ')==0) str = str.substring(1); if (flag!=1) while ((str.length>0)&&(str.lastIndexOf(' ')==str.length-1)) str = str.substring(0,str.length-1); return str;} http://www.csdn.net/expert/topic/662/662799.xml?temp=.5201074 indexOffunction hasBlank(str){return str.indexOf(" ")!=-1;}正则function hasBlank(str){var reg=/[^ ]/return !reg.test(str);} 正则应该是function hasBlank(str){return / /.test(str);} 一点补充:我上面贴出的代码,是为String对象添加了几个方法。利用prototype属性可以为对象添加方法,很爽 嗯,不错,不错,谢谢各位了. to Andrawu(Andrawu): 是了,CharAt是更合我习惯的东东.:) to weidegong: 受益非浅.只是replace的第一个参数不懂,看来要找本jsp的书看看了. 谢谢大家的帮忙. 买单. 浏览器及其交互性的对比 Jquery为多级下的标签添加click事件 请教一个正则问题 AjaxPro在外部js文件中使用问题 FCKEditor 在模态窗里的一个BUG 找了半天找不到错误,大家帮忙 如何将新开窗体的大小设置为屏幕的大小??? 怎样实现IE菜单中的目标另存为? javascript的計算機eval 用js怎么在网页中实现多图片自由移动 插入字符在光标之后 怎样将javascript里的变量值显示在html页面中?
象VBS里的,trim(str)..
<input type=button value=检测 onclick= 'if(document.all("show").value.indexOf(" ") >=0) alert("有空格");else alert("OK.")' >
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}String.prototype.getLength = function(){
var l=this.length;
var n=l
for (var i=0;i<l;i++){
if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++
}
return n
}sStr=new String("")
alert("["+sStr+"]")
alert("["+sStr.LTrim()+"]")
alert("["+sStr.RTrim()+"]")
alert("["+sStr.Trim()+"]")
alert(sStr.RealLength());
</script>
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}String.prototype.getLength = function(){
var l=this.length;
var n=l
for (var i=0;i<l;i++){
if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++
}
return n
}sStr=new String("")
alert("["+sStr+"]")
alert("["+sStr.LTrim()+"]")
alert("["+sStr.RTrim()+"]")
alert("["+sStr.Trim()+"]")
alert(sStr.RealLength());
</script>
只是还是没有trim来的清晰,不好意思我这样说.
也许我只能自己写个function,先,indexof,再lastindexof,再substr.....
我是想把前后的空格都去掉.请大家多帮忙.
* 去掉字符串左/右端空格
* @param :str 字符串;flag 控制标志:0 去掉两端空格,缺省值、1 去掉左端空格、2 去掉右端空格
* @return :去掉前后空格的String对象。
*/
function trim(str,flag) {
if (!flag) flag = 0;
if (flag!=2)
while (str.indexOf(' ')==0)
str = str.substring(1);
if (flag!=1)
while ((str.length>0)&&(str.lastIndexOf(' ')==str.length-1))
str = str.substring(0,str.length-1);
return str;
}
function hasBlank(str){
return str.indexOf(" ")!=-1;
}
正则
function hasBlank(str){
var reg=/[^ ]/
return !reg.test(str);
}
return / /.test(str);
}
to Andrawu(Andrawu): 是了,CharAt是更合我习惯的东东.:)
to weidegong: 受益非浅.只是replace的第一个参数不懂,看来要找本jsp的书看看了.
谢谢大家的帮忙.
买单.