根据下拉列表值选择,将电子邮件字段设为必填字段 [英] Make the email field required based on the dropdown list value selection

查看:132
本文介绍了根据下拉列表值选择,将电子邮件字段设为必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在mvc 3中

我的视图中有一个ddlfor,其中列出了两个值A和B. 同一视图中还有另外一个字段电子邮件 仅当ddl的选定值为'A'时,我才需要将此电子邮件字段设为必填字段或必填字段,并且无需验证选定的值是否为'B'. 我该如何实施这种情况.PLsss可以帮助我

现在在我的模型中,电子邮件是必填字段.我需要改变吗? 如果我更改了该位置,那么我将根据ddl值将电子邮件设置为强制性还是非强制性?

请给我建议. 提前致谢, 普里亚(Priya)

解决方案

您可以在JQuery中做到这一点.在Jquery中,如果"A"和电子邮件字段为空,请检查drop down的值,返回false,以便不提交表单.

在服务器端,您可以使用自定义方法进行验证,而不必使用数据注释.

My application is in mvc 3

I have a ddlfor in my view which lists two values A and B. There is one more field email in same view I need to make this email field a required or mandatory filed only if the selected value of the ddl is 'A'.And no need to validate if the value selected is 'B'. How can i implement this scenario.PLsss help me

Now in my model, email is a mandatory field. Do i need to change that? If i change that where i will make email mandatory and not mandatory based on ddl value?

Pls advice me. Thanks in advance, Priya

解决方案

You can do that in JQuery. In Jquery check the value of drop down if 'A' and email field is empty return false so that form will not be submitted.

On server side rather than using data annotation you can have a custom method to validate.

这篇关于根据下拉列表值选择,将电子邮件字段设为必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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