鼠标离开的去掉输入字符前后空格。赶集用户注册就是这样的,还有就是一个汉字两个字符,我规定输入2至20个字符,输入一个汉字就可以了。可还得输入2个汉字,
blur掉的方法是function checkC_USER_ID(C_USER_ID){
PubicObj = $('#C_USER_ID').get(0);
if(C_USER_ID.value==""){
document.getElementById("C_USER_IDTip").innerHTML=form_chk_Key[0][1];
$("#C_USER_IDTip").removeClass().addClass("onError");
//document.getElementById("C_USER_IDTip").className="onError";

}
else if(!(renyi).test(C_USER_ID.value)){
//alert(3);
document.getElementById("C_USER_IDTip").innerHTML=form_chk_ID[1];
$("#C_USER_IDTip").removeClass().addClass("onError");
//document.getElementById("C_USER_IDTip").className="onError";

}
else if(!(USER_ID2).test(C_USER_ID.value)){//alert(2);
document.getElementById("C_USER_IDTip").innerHTML=form_chk_ID[2];
$("#C_USER_IDTip").removeClass().addClass("onError");
//document.getElementById("C_USER_IDTip").className="onError";

}
else{
$.ajax({
   type: "POST",
   url: "/servlet/AjaxManager",
   dataType: "text",
   contentType: "application/x-www-form-urlencoded;charset=utf-8",
   data: "HD_STATS=C_CK_NAME&CHECK_ID=" + "chk_userID"+"&FLD_VALUE="+C_USER_ID.value,
   beforeSend: function(XMLHttpRequest) {   
   $("#C_USER_IDTip").html("<img src = '/IMG/gif002.gif' title = '正在验证用户名……' />");  
   },
   success: function(msg) {  
 //  $("#C_USER_IDTip").html(" ").removeClass().addClass("onCorrect");
sendRequestByAjaxPost_Handler_Jquery(msg,userLinkCallBack);   //msg :返回的xml形式的字符串    CustApplCallBackAppl  需要回调的一个方法
   },
   error: function(jqXHR, textStatus, errorThrown) {
    alert("系统繁忙,请稍后再试……");   
   // $("#C_USER_IDTip").html("");  
   }
   });   
}
}找了个去前后空格的方法,不知道该怎么用。function trim(str){   
    return str.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'');   

$("#C_USER_NAME").blur(function(){
checkC_USER_NAME(this);
})

解决方案 »

  1.   


    function trim(str){   
        return str.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'');   

    var aa=" 12312 ";
    aa =trim(aa);
      

  2.   

    1、trim(str)这个去掉空格的方法直接在js调用啊,把要验证的字符串作为参数传进去
    2、根据字符的ascii码来验证是否是中文 ,大于127是中文。 
    eg:
    if (charCodeAt(i)>127) {  
                 len2 += 2;  
             } else {  
                 len2 ++;  
             }  
      

  3.   

    function checkC_USER_ID(C_USER_ID){
    alert(C_USER_ID.value);
    C_USER_ID=trims(C_USER_ID.value);
    alert("去空格之后="+C_USER_ID.value);
    PubicObj = $('#C_USER_ID').get(0);
    if(C_USER_ID.value==""){
    document.getElementById("C_USER_IDTip").innerHTML=form_chk_Key[0][1];
    $("#C_USER_IDTip").removeClass().addClass("onError");
    //document.getElementById("C_USER_IDTip").className="onError";

    }第一个alert可以弹出输入的值,第二个弹出“去空格之后=undefined”
      

  4.   

    我写错了。去空格好了。function checkC_USER_ID(C_USER_ID){
        alert(C_USER_ID.value);
        C_USER_ID=trims(C_USER_ID.value);
        alert("去空格之后="+C_USER_ID);
       
    应该是这样,可文本里的值并没有变化,比如输入:【   123】,弹出来【123】但是文本里的值还是【   123】,我想让文本离得值也是【123】
      

  5.   

    function checkC_USER_ID(C_USER_ID){
    //alert(C_USER_ID.value);
    C_USER_ID=trims(C_USER_ID.value);
    //alert("去空格之后="+C_USER_ID);
    $("#C_USER_ID").val(C_USER_ID);
    if (charCodeAt(C_USER_ID)>127) {  
            len2 += 2;  
        } else {  
            len2 ++;  
        } 

    PubicObj = $('#C_USER_ID').get(0);
    if(C_USER_ID==""){
    document.getElementById("C_USER_IDTip").innerHTML=form_chk_Key[0][1];
    $("#C_USER_IDTip").removeClass().addClass("onError");
    //document.getElementById("C_USER_IDTip").className="onError";

    }这样??可不对,能讲解仔细点不
      

  6.   


    function checkC_USER_ID(C_USER_ID){    
        //alert(C_USER_ID.value);
        C_USER_ID=trims(C_USER_ID.value);
        //alert("去空格之后="+C_USER_ID);
        $("#C_USER_ID").val(C_USER_ID);
        var len = C_USER_ID.replace(/[^\x00-\xff]/g,"**").length;
        if (len > 20 || len < 2) {  
            //alert("2-20个字符");
        } 
         
        PubicObj = $('#C_USER_ID').get(0);
    if(C_USER_ID==""){
        document.getElementById("C_USER_IDTip").innerHTML=form_chk_Key[0][1];
        $("#C_USER_IDTip").removeClass().addClass("onError");
        //document.getElementById("C_USER_IDTip").className="onError";
             
        }
      

  7.   


    public static void main(String[] args) throws Exception {
    String s = "aa";
    String s1 = "啊";
    System.out.println(s.length());
    System.out.println(s1.length());
    }
    运行结果很明显是 2,1  虽然中文是站两个字符,但是你直接取length肯定获取出来是1,你需要先转成char[]数组才行的