学号()        * 半角英数,最长6位  
姓名 ()       * 最长15位 
如果输入的内容不符合提示的话,我就想把后面的那些提示改成像学号不能为空 姓名不能为空之类文字
我在js里面应该怎么控制?

解决方案 »

  1.   

    if 字符串的长度<规定的长度
    alert();
      

  2.   

    <%@ page language="java" pageEncoding="GBK"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP 'MyJsp.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="jslib/jquery-1.4.2.js"></script>
    </head> <body>
    学号:<input type="text" id="num" onblur="check(this)"><em id="num_msg" style="color:red">*半角英数,最长6位</em>
    </body>
    <script type="text/javascript" language="JavaScript">
    function check(obj){
    var id = obj.id;
    var em = document.getElementById(id+"_msg");
    var txt = obj.value;
    if(!txt || txt.replace(/^\s*|\s*$/,'').length==0){
    em.style.color="red";
    em.innerText = "学号不能为空!"
    }else if(!/^[0-9]+$/.test(txt)){
    em.style.color="red";
    em.innerText = "学号由半角英文数字组成!"
    }else if(txt.length>6){
    em.style.color="red";
    em.innerText = "学号最长6位!"
    }else{
    em.style.color="green";
    em.innerText = "OK!"
    }
    }
    </script>
    </html>是这样么?