日期:2014-05-16  浏览次数:20340 次

如何匹配部分字符
if (theForm.city.value == "--请选择--")
  {
    alert(" 请选择 \"区域\" ,。");
    theForm.city.focus();
    return (false);
  }

比如,利用上面的,如何判断,name=a1里面的字符必须包含http
还有,利用上面的格式,如何判断name=2a里面的必须为正整数或者0.

------解决方案--------------------
必须包含http可以用str.indexOf('http')!=-1
包含正整数可以用正则/\d+/.test(str)
------解决方案--------------------
    if (theForm.city.value == "--请选择--"){
        alert(" 请选择 \"区域\" ,。");
        theForm.city.focus();
        return false;
    }else if (~theForm.a1.value.indexOf("http")){
        alert(" a1里面的字符必须包含http");
        theForm.a1.focus();
        return false;
    }else if (/^0$
------解决方案--------------------
^[1-9]\d*$/.test(theForm.a2.value)){
        alert("a2里面的必须为正整数或者0");
        theForm.a2.focus();
        return false;
    }

注:不推荐theForm.a1.value这种不规范写法。
------解决方案--------------------
更正为:
}else if (!~theForm.a1.value.indexOf("http")){