g418ouxiaocong 2008-08-01 22:35
浏览 267
已采纳

在struts中,怎么用javascript验证啊?

在struts中,怎么用javascript验证啊?我在JSP页面里面,写了这些代码:
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>


JSP for DynaValidatorForm form




帐号:

密码:
        <html:submit/><html:cancel/>
    </html:form>
</body>


然后这些代码是发送到actionform里面~但运行的时候,在这个JSP页面里,如果我不输入帐号的话,它不会提示错误,这是什么问题??急啊!!请高手们指点一下!!小弟万分感谢啊!帮我改改代码~或者发一些代码给我参考参考!!急啊!!
[b]问题补充:[/b]
我不是想在没有输入帐号和密码的情况下弹出对话框,而是在后面显示一些红色的字,怎么解决呢??

  • 写回答

3条回答 默认 最新

  • sunjun041640 2008-08-03 16:54
    关注

    可以用"我佛山人"的validator.js, 非常好用,可以设置出错后的显示模式

    [code="java"]

    表单验证类 Validator v1.05
    <br> body,td{font:normal 12px Verdana;color:#333333}<br> input,textarea,select,td{font:normal 12px Verdana;color:#333333;border:1px solid #999999;background:#ffffff}<br> table{border-collapse:collapse;}<br> td{padding:3px}<br> input{height:20;}<br> textarea{width:80%;height:50px;overflow:auto;}<br> form{display:inline}<br>












































































    身份证号:
    真实姓名:
    ID:
    英文名:
    主页:
    密码:
    重复:
    信箱:
    信箱:
    QQ:
    身份证:
    年龄:
    年龄1:
    电话:
    手机:
    生日:
    邮政编码:
    邮政编码:
    操作系统: 选择您所用的操作系统Win98Win2kWinXP
    所在省份: 广东陕西浙江江西
    爱好: 运动上网听音乐看书
    自我介绍: 中文是一个字 自传: 中文是两个字节t
    相片上传:

    <br> /*************************************************<br> Validator v1.05<br> code by 我佛山人<br> <a href="mailto:wfsr@msn.com">wfsr@msn.com</a><br> *************************************************/<br> Validator = {<br> Require : /.+/,<br> Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*$/,<br> Phone : /^(((\d{2,3}))|(\d{3}-))?((0\d{2,3})|0\d{2,3}-)?[1-9]\d{6,7}(-\d{1,4})?$/,<br> Mobile : /^(((\d{2,3}))|(\d{3}-))?13\d{9}$/,<br> Url : /^http:\/\/[A-Za-z0-9]+.[A-Za-z0-9]+[\/=\?%-&amp;_~`@[]\&#39;:+!]*([^&lt;&gt;\&quot;\&quot;])*$/,<br> IdCard : &quot;this.IsIdCard(value)&quot;,<br> Currency : /^\d+(.\d+)?$/,<br> Number : /^\d+$/,<br> Zip : /^[1-9]\d{5}$/,<br> QQ : /^[1-9]\d{4,8}$/,<br> Integer : /^[-+]?\d+$/,<br> Double : /^[-+]?\d+(.\d+)?$/,<br> English : /^[A-Za-z]+$/,<br> Chinese : /^[\u0391-\uFFE5]+$/,<br> Username : /^[a-z]\w{3,}$/i,<br> UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_~!@#\$%^&amp;*.()[]{}&lt;&gt;\?\\/\&#39;\&quot;]*)|.{0,5})$|\s/,<br> IsSafe : function(str){return !this.UnSafe.test(str);},<br> SafeString : &quot;this.IsSafe(value)&quot;,<br> Filter : &quot;this.DoFilter(value, getAttribute(&#39;accept&#39;))&quot;,<br> Limit : &quot;this.limit(value.length,getAttribute(&#39;min&#39;), getAttribute(&#39;max&#39;))&quot;,<br> LimitB : &quot;this.limit(this.LenB(value), getAttribute(&#39;min&#39;), getAttribute(&#39;max&#39;))&quot;,<br> Date : &quot;this.IsDate(value, getAttribute(&#39;min&#39;), getAttribute(&#39;format&#39;))&quot;,<br> Repeat : &quot;value == document.getElementsByName(getAttribute(&#39;to&#39;))[0].value&quot;,<br> Range : &quot;getAttribute(&#39;min&#39;) &lt; (value|0) &amp;&amp; (value|0) &lt; getAttribute(&#39;max&#39;)&quot;,<br> Compare : &quot;this.compare(value,getAttribute(&#39;operator&#39;),getAttribute(&#39;to&#39;))&quot;,<br> Custom : &quot;this.Exec(value, getAttribute(&#39;regexp&#39;))&quot;,<br> Group : &quot;this.MustChecked(getAttribute(&#39;name&#39;), getAttribute(&#39;min&#39;), getAttribute(&#39;max&#39;))&quot;,<br> ErrorItem : [document.forms[0]],<br> ErrorMessage : [&quot;以下原因导致提交失败:\t\t\t\t&quot;],<br> Validate : function(theForm, mode){<br> var obj = theForm || event.srcElement;<br> var count = obj.elements.length;<br> this.ErrorMessage.length = 1;<br> this.ErrorItem.length = 1;<br> this.ErrorItem[0] = obj;<br> for(var i=0;i<count;i++){<br> with(obj.elements[i]){<br> var _dataType = getAttribute("dataType");<br> if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined") continue;<br> this.ClearState(obj.elements[i]);<br> if(getAttribute("require") == "false" && value == "") continue;<br> switch(_dataType){<br> case "IdCard" :<br> case "Date" :<br> case "Repeat" :<br> case "Range" :<br> case "Compare" :<br> case "Custom" :<br> case "Group" : <br> case "Limit" :<br> case "LimitB" :<br> case "SafeString" :<br> case "Filter" :<br> if(!eval(this[_dataType])) {<br> this.AddError(i, getAttribute("msg"));<br> }<br> break;<br> default :<br> if(!this[_dataType].test(value)){<br> this.AddError(i, getAttribute("msg"));<br> }<br> break;<br> }<br> }<br> }<br> if(this.ErrorMessage.length > 1){<br> mode = mode || 1;<br> var errCount = this.ErrorItem.length;<br> switch(mode){<br> case 2 :<br> for(var i=1;i<errCount;i++)<br> this.ErrorItem[i].style.color = "red";<br> case 1 :<br> alert(this.ErrorMessage.join("\n"));<br> this.ErrorItem[1].focus();<br> break;<br> case 3 :<br> for(var i=1;i<errCount;i++){<br> try{<br> var span = document.createElement("SPAN");<br> span.id = "__ErrorMessagePanel";<br> span.style.color = "red";<br> this.ErrorItem[i].parentNode.appendChild(span);<br> span.innerHTML = this.ErrorMessage[i].replace(/\d+:/,"*");<br> }<br> catch(e){alert(e.description);}<br> }<br> this.ErrorItem[1].focus();<br> break;<br> default :<br> alert(this.ErrorMessage.join("\n"));<br> break;<br> }<br> return false;<br> }<br> return true;<br> },<br> limit : function(len,min, max){<br> min = min || 0;<br> max = max || Number.MAX_VALUE;<br> return min <= len && len <= max;<br> },<br> LenB : function(str){<br> return str.replace(/[^\x00-\xff]/g,"**").length;<br> },<br> ClearState : function(elem){<br> with(elem){<br> if(style.color == "red")<br> style.color = "";<br> var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];<br> if(lastNode.id == "__ErrorMessagePanel")<br> parentNode.removeChild(lastNode);<br> }<br> },<br> AddError : function(index, str){<br> this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];<br> this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;<br> },<br> Exec : function(op, reg){<br> return new RegExp(reg,"g").test(op);<br> },<br> compare : function(op1,operator,op2){<br> switch (operator) {<br> case "NotEqual":<br> return (op1 != op2);<br> case "GreaterThan":<br> return (op1 > op2);<br> case &quot;GreaterThanEqual&quot;:<br> return (op1 &gt;= op2);<br> case &quot;LessThan&quot;:<br> return (op1 &lt; op2);<br> case &quot;LessThanEqual&quot;:<br> return (op1 &lt;= op2);<br> default:<br> return (op1 == op2);<br><br> }<br> },<br> MustChecked : function(name, min, max){<br> var groups = document.getElementsByName(name);<br> var hasChecked = 0;<br> min = min || 1;<br> max = max || groups.length;<br> for(var i=groups.length-1;i&gt;=0;i--)<br> if(groups[i].checked) hasChecked++;<br> return min &lt;= hasChecked &amp;&amp; hasChecked &lt;= max;<br> },<br> DoFilter : function(input, filter){<br> return new RegExp(&quot;^.+.(?=EXT)(EXT)$&quot;.replace(/EXT/g, filter.split(/\s*,\s*/).join(&quot;|&quot;)), &quot;gi&quot;).test(input);<br> },<br> IsIdCard : function(number){<br> var date, Ai;<br> var verify = &quot;10x98765432&quot;;<br> var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];<br> var area = [&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;北京&#39;,&#39;天津&#39;,&#39;河北&#39;,&#39;山西&#39;,&#39;内蒙古&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;辽宁&#39;,&#39;吉林&#39;,&#39;黑龙江&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;上海&#39;,&#39;江苏&#39;,&#39;浙江&#39;,&#39;安微&#39;,&#39;福建&#39;,&#39;江西&#39;,&#39;山东&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;河南&#39;,&#39;湖北&#39;,&#39;湖南&#39;,&#39;广东&#39;,&#39;广西&#39;,&#39;海南&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;重庆&#39;,&#39;四川&#39;,&#39;贵州&#39;,&#39;云南&#39;,&#39;西藏&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;陕西&#39;,&#39;甘肃&#39;,&#39;青海&#39;,&#39;宁夏&#39;,&#39;新疆&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;台湾&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;香港&#39;,&#39;澳门&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;国外&#39;];<br> var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);<br> if(re == null) return false;<br> if(re[1] &gt;= area.length || area[re[1]] == &quot;&quot;) return false;<br> if(re[2].length == 12){<br> Ai = number.substr(0, 17);<br> date = [re[9], re[10], re[11]].join(&quot;-&quot;);<br> }<br> else{<br> Ai = number.substr(0, 6) + &quot;19&quot; + number.substr(6);<br> date = [&quot;19&quot; + re[4], re[5], re[6]].join(&quot;-&quot;);<br> }<br> if(!this.IsDate(date, &quot;ymd&quot;)) return false;<br> var sum = 0;<br> for(var i = 0;i&lt;=16;i++){<br> sum += Ai.charAt(i) * Wi[i];<br> }<br> Ai += verify.charAt(sum%11);<br> return (number.length ==15 || number.length == 18 &amp;&amp; number == Ai);<br> },<br> IsDate : function(op, formatString){<br> formatString = formatString || &quot;ymd&quot;;<br> var m, year, month, day;<br> switch(formatString){<br> case &quot;ymd&quot; :<br> m = op.match(new RegExp(&quot;^((\d{4})|(\d{2}))([-./])(\d{1,2})\4(\d{1,2})$&quot;));<br> if(m == null ) return false;<br> day = m[6];<br> month = m[5]*1;<br> year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));<br> break;<br> case &quot;dmy&quot; :<br> m = op.match(new RegExp(&quot;^(\d{1,2})([-./])(\d{1,2})\2((\d{4})|(\d{2}))$&quot;));<br> if(m == null ) return false;<br> day = m[1];<br> month = m[3]*1;<br> year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));<br> break;<br> default :<br> break;<br> }<br> if(!parseInt(month)) return false;<br> month = month==0 ?12:month;<br> var date = new Date(year, month-1, day);<br> return (typeof(date) == &quot;object&quot; &amp;&amp; year == date.getFullYear() &amp;&amp; month == (date.getMonth()+1) &amp;&amp; day == date.getDate());<br> function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}<br> }<br> }<br> [/code]
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决