从下拉列表中验证选项 [英] Validate an option from a dropdown

查看:93
本文介绍了从下拉列表中验证选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的下拉列表中有8个安全问题可供选择,其中一个是你的出生日期是什么时候

只有选择了这个选项,我才希望它能够验证并确保DOB以正确的格式输入,即08/07/1980,并在验证摘要中显示错误消息。我是否使用自定义验证器?请有人帮帮我吗?



我真的很擅长开发,所以请放轻松我:)



谢谢!



我尝试过:



<% - < asp:RegularExpressionValidator ID =ValDOBDisplay =NoneEnableClientScript =falseErrorMessage =请输入有效的DOBrunat =serverControlToValidate =txtQ1ValidationExpression =^( ((0 [1-9] | [12] \d | 3 [01])\ /(0 [13578] | 1 [02])\ /((19 | [2-9] \d )\d {2}))|((0 [1-9] | [12] \d | 30)\ /(0 [13456789] | 1 [012])\ /((19 | [ 2-9] \d)\d {2}))|((0 [1-9] | 1\d | 2 [0-8])\ / 02\ /((19 | [ 2-9] \d)\d {2}))|(29\ / 02\ /((1 [6-9] | [2-9] \d)(0 [48] | [2468] [048] | [13579] [26])|((16 | [2468] [048] | [3579] [26])00))))$> - %>

My dropdown has 8 security question to select from, one of which is "What is your date of birth"
Only if this option is selected, I would like it to validate and make sure that DOB is entered in the correct format i.e 08/07/1980 and for the error message to display in the validation summary. Do I use a custom validator? please can someone help me?

I'm really new to development, so please go easy on me :)

Thank you!

What I have tried:

<%--<asp:RegularExpressionValidator ID="ValDOB" Display="None" EnableClientScript="false" ErrorMessage="Please enter a valid DOB" runat="server" ControlToValidate="txtQ1" ValidationExpression="^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$">--%>

推荐答案

> - %>
">--%>


不要使用正则表达式进行日期验证 - 它太复杂了。即使没有理查德提到的格式问题,哇t将在闰年发生?正则表达式不擅长处理数据处理 - 它们是字符串处理器而不是别的。

相反,使用 Date.parse() - JavaScript | MDN [ ^ ]通过代码。
Don't use a regex for date validation - it's far too complex. Even without the format problem Richard mentions, what is going to happen on a leap year? Regexes aren't good at coping with data processing - they are string processors and nothing else.
Instead, validate the date in Javascript using Date.parse() - JavaScript | MDN[^] via code.


这篇关于从下拉列表中验证选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆