hi im sure this is a basic question for you guys, but i need some help validating two fields so for example say one field has an input in i dont need to validate the other vice versa,
this is my HTML mark up and im focusing on the 2 fields from the last on which are "Where did you see the vacancy advertised?" and "Who recommended you"
<div id="ValidationSummary">
<%= Html.ValidationSummary("Please fix the following problems") %>
</div>
<% using (Html.BeginForm("job_application", "vacancies", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<div class="blue-border-form-details">
<h1>Personal Details</h1>
<fieldset>
<legend><label>* Title:</label><%=Html.DropDownList("Title", Model.TitleCollection())%></legend>
<legend><label>* First name:</label><%=Html.TextBox("FirstName", Model.FirstName, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend>
<legend><label>* Surname:</label><%=Html.TextBox("SurName", Model.SurName, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend>
<legend><label>* Email:</label><%=Html.TextBox("EmailAddress", Model.EmailAddress, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend>
<legend><label>* Mobile:</label><%=Html.TextBox("Mobilenumber", Model.Mobilenumber, new { maxlength = 11, size = "30", @class = "number", autocomplete = "off" })%></legend>
<legend><label>* Work Eligibility:</label> <%= Html.RadioButton("EligibleToWork", Model.EligibleToWork,true)%> I confirm that I am eligible to live and work in the UK</legend>
<legend><label> </label> <%= Html.RadioButton("NotEligibleToWork", Model.NotEligibleToWork,false)%> I am not currently eligible to live or work in the UK</legend>
<h1>Application Details</h1>
<div class="white-space"></div>
<legend><label>* Title:</label><%=Html.DropDownList("JobTitles", Model.VacanciesCollection())%></legend>
<legend><label>* Location:</label><%=Html.DropDownList("Locations", Model.LocationsCollection())%></legend>
<legend><label>* Attach CV:</label>How to upload your CV. Just click on the browse button below, find your CV in your<br /> files and add it. We can accept PDF or Word documents no larger than 500kb.
<br /></legend>
<legend><label> </label>Alternatively copy and paste your CV into the covering letter area.</legend>
<legend><label> </label><%= Html.TextBoxFor(m=> m.CV, new { type= "file" }) %><br /></legend>
<legend><label>Cover letter:</label><%= Html.TextArea("CoverLetter", Model.CoverLetter, new { maxlength = 2000, size = "300", @class = "text-area-enquiries", autocomplete = "off" })%></legend>
<legend><label class="large-label">Where did you see<br /> the vacancy Advertised?</label><%= Html.TextBox("VacancyAdvertised", Model.VacancyAdvertised, new { maxlength = 30, size = "30", autocomplete = "off" })%> </legend>
<legend><label class="large-label">Who recommended you?</label><%= Html.TextBox("RecommendedYou", Model.RecommendedYou, new { maxlength = 30, size = "30", autocomplete = "off" })%></legend>
<legend><label> </label><%= Html.CheckBox("CopyofApplication",Model.CopyofApplication)%> I wish to receive a copy of this application.</legend>
</fieldset>
</div>
<div class="white-space"></div>
<div>
<span class="margin-left"><a href="/vacancies/current-vacancies"><img src="/img/buttons/Back-to-Job-Description-Button.png" alt="Click to go back to Job Description" class="margin-left" title="Back to current vacancies"/></a></span>
<span><input type="submit" name="Submit" id="Submit" class="offset6 apply-now-button submit" value="Apply Now" title="Apply now | Send CV & application" runat="server" /></span>
</div>
<%= Html.ClientSideValidation("", typeof(BpdJobsModal.JobApplication))%>
<% } %>
</div>
This is the code in the modal again how can i combinded both of these in to one, make sure one of the two fields have a value in
[Required(ErrorMessage = " * Required")]
[RegularExpression(".{2,}", ErrorMessage = "Invalid Required Field")]
public string VacancyAdvertised { get; set; }
[Required(ErrorMessage = " * Required")]
[RegularExpression(".{2,}", ErrorMessage = "Invalid Required Field")]
public string RecommendedYou { get; set; }
Any help will be highly appreciated.
Thank you
Check out the Mvc.ValidationToolkit which has a RequiredEmptyIfAttribute. You would apply RequiredEmptyIf("OtherField", "TargetValue") to both the textbox inputs.
iv downloaded the file from the link you provided but i cand find the file i need to import in to my project to use the solution you provided...........
If you download the attachment of the link that Norton posted to you, you will find RequiredIfEmptyAttribute.cs under the root of Mvc.Toolkit. It is a custom attribute that the author defined, it will meet your requirement. So you just need
to copy this code into your project and use it like RequiredEmptyIf("OtherField", "TargetValue").
Hope this helpful
Regards
Young Yang
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Harrison.Sco...
Member
420 Points
530 Posts
How to validate two fields
Mar 19, 2012 09:41 AM|LINK
hi im sure this is a basic question for you guys, but i need some help validating two fields so for example say one field has an input in i dont need to validate the other vice versa,
this is my HTML mark up and im focusing on the 2 fields from the last on which are "Where did you see the vacancy advertised?" and "Who recommended you"
<div id="ValidationSummary"> <%= Html.ValidationSummary("Please fix the following problems") %> </div> <% using (Html.BeginForm("job_application", "vacancies", FormMethod.Post, new { enctype = "multipart/form-data" })) { %> <div class="blue-border-form-details"> <h1>Personal Details</h1> <fieldset> <legend><label>* Title:</label><%=Html.DropDownList("Title", Model.TitleCollection())%></legend> <legend><label>* First name:</label><%=Html.TextBox("FirstName", Model.FirstName, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Surname:</label><%=Html.TextBox("SurName", Model.SurName, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Email:</label><%=Html.TextBox("EmailAddress", Model.EmailAddress, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Mobile:</label><%=Html.TextBox("Mobilenumber", Model.Mobilenumber, new { maxlength = 11, size = "30", @class = "number", autocomplete = "off" })%></legend> <legend><label>* Work Eligibility:</label> <%= Html.RadioButton("EligibleToWork", Model.EligibleToWork,true)%> I confirm that I am eligible to live and work in the UK</legend> <legend><label> </label> <%= Html.RadioButton("NotEligibleToWork", Model.NotEligibleToWork,false)%> I am not currently eligible to live or work in the UK</legend> <h1>Application Details</h1> <div class="white-space"></div> <legend><label>* Title:</label><%=Html.DropDownList("JobTitles", Model.VacanciesCollection())%></legend> <legend><label>* Location:</label><%=Html.DropDownList("Locations", Model.LocationsCollection())%></legend> <legend><label>* Attach CV:</label>How to upload your CV. Just click on the browse button below, find your CV in your<br /> files and add it. We can accept PDF or Word documents no larger than 500kb. <br /></legend> <legend><label> </label>Alternatively copy and paste your CV into the covering letter area.</legend> <legend><label> </label><%= Html.TextBoxFor(m=> m.CV, new { type= "file" }) %><br /></legend> <legend><label>Cover letter:</label><%= Html.TextArea("CoverLetter", Model.CoverLetter, new { maxlength = 2000, size = "300", @class = "text-area-enquiries", autocomplete = "off" })%></legend> <legend><label class="large-label">Where did you see<br /> the vacancy Advertised?</label><%= Html.TextBox("VacancyAdvertised", Model.VacancyAdvertised, new { maxlength = 30, size = "30", autocomplete = "off" })%> </legend> <legend><label class="large-label">Who recommended you?</label><%= Html.TextBox("RecommendedYou", Model.RecommendedYou, new { maxlength = 30, size = "30", autocomplete = "off" })%></legend> <legend><label> </label><%= Html.CheckBox("CopyofApplication",Model.CopyofApplication)%> I wish to receive a copy of this application.</legend> </fieldset> </div> <div class="white-space"></div> <div> <span class="margin-left"><a href="/vacancies/current-vacancies"><img src="/img/buttons/Back-to-Job-Description-Button.png" alt="Click to go back to Job Description" class="margin-left" title="Back to current vacancies"/></a></span> <span><input type="submit" name="Submit" id="Submit" class="offset6 apply-now-button submit" value="Apply Now" title="Apply now | Send CV & application" runat="server" /></span> </div> <%= Html.ClientSideValidation("", typeof(BpdJobsModal.JobApplication))%> <% } %> </div> This is the code in the modal again how can i combinded both of these in to one, make sure one of the two fields have a value in [Required(ErrorMessage = " * Required")] [RegularExpression(".{2,}", ErrorMessage = "Invalid Required Field")] public string VacancyAdvertised { get; set; } [Required(ErrorMessage = " * Required")] [RegularExpression(".{2,}", ErrorMessage = "Invalid Required Field")] public string RecommendedYou { get; set; } Any help will be highly appreciated. Thank youDarrellNorto...
All-Star
87443 Points
9718 Posts
Moderator
MVP
Re: How to validate two fields
Mar 19, 2012 09:48 AM|LINK
Check out the Mvc.ValidationToolkit which has a RequiredEmptyIfAttribute. You would apply RequiredEmptyIf("OtherField", "TargetValue") to both the textbox inputs.
http://blogs.msdn.com/b/simonince/archive/2011/09/29/mvc-validationtookit-alpha-release-conditional-validation-with-mvc-3.aspx
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Harrison.Sco...
Member
420 Points
530 Posts
Re: How to validate two fields
Mar 19, 2012 09:53 AM|LINK
Could i not just use a IF Statement? ill take a look at the link though
Harrison.Sco...
Member
420 Points
530 Posts
Re: How to validate two fields
Mar 19, 2012 10:05 AM|LINK
iv downloaded the file from the link you provided but i cand find the file i need to import in to my project to use the solution you provided...........
Harrison.Sco...
Member
420 Points
530 Posts
Re: How to validate two fields
Mar 19, 2012 11:22 AM|LINK
this is my current modal
public class JobApplication
{
#region " Properties "
[Required(ErrorMessage = " * Required")]
public string Title { get; set; }
[Required(ErrorMessage = " * Required")]
public string JobTitles {get;set;}
[Required(ErrorMessage = " * Required")]
public string Locations { get; set; }
[Required(ErrorMessage=" * Required")]
[RegularExpression(".{2,}",ErrorMessage="Invalid First name")]
public string FirstName { get; set; }
[Required(ErrorMessage = " * Required")]
[RegularExpression(".{2,}", ErrorMessage = "Invalid Surname")]
public string SurName { get; set; }
[Required(ErrorMessage = " Required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid e-mail address")]
public string EmailAddress { get; set; }
[Required(ErrorMessage = "* Required")]
[RegularExpression("^0.{10}", ErrorMessage = "Invalid Mobile number")]
public string Mobilenumber { get; set; }
public bool EligibleToWork { get; set; }
public bool NotEligibleToWork { get; set; }
public bool CopyofApplication { get; set; }
public string CoverLetter { get; set; }
[Required(ErrorMessage = " * Required")]
[RegularExpression(".{2,}", ErrorMessage = "Invalid Required Field")]
public string VacancyAdvertised { get; set; }
[Required(ErrorMessage = " * Required")]
[RegularExpression(".{2,}", ErrorMessage = "Invalid Required Field")]
public string RecommendedYou { get; set; }
public JobApplication()
{
}
public Dictionary<string, string> TitleCollection()
{
Dictionary<string,string> titles = new Dictionary<string,string>
{
{"","Select"},
{"Mr","Mr"},
{"Mrs","Mrs"},
{"Miss","Miss"},
{"Ms","Ms"}
};
return titles;
}
public Dictionary<string, string> VacanciesCollection()
{
Dictionary<string, string> Vacanciestitles = new Dictionary<string, string>
{
{"","Select"},
{"Telesales-Account-Managers","Telesales Account Managers"},
{"Field-Sales-Executives","Field Sales Executives"},
{"Trainee-Sales","Trainee Sales"}
};
return Vacanciestitles;
}
public Dictionary<string, string> LocationsCollection()
{
Dictionary<string, string> LocationsName = new Dictionary<string, string>
{
{"","Select"},
{"Whiteley","Whiteley"},
{"Birmingham","Birmingham"},
{"Glasgow","Glasgow"},
{"Southampton","Southampton"},
{"London","London"}
};
return LocationsName;
}
public HttpPostedFileBase CV { get; set; }
#endregion
}
not im not to sure what part of the download you provided i need to add in to my project so i can use RequiredEmptyIf any help please?
Young Yang -...
All-Star
21741 Points
1825 Posts
Microsoft
Re: How to validate two fields
Mar 26, 2012 02:59 AM|LINK
Hi
If you download the attachment of the link that Norton posted to you, you will find RequiredIfEmptyAttribute.cs under the root of Mvc.Toolkit. It is a custom attribute that the author defined, it will meet your requirement. So you just need to copy this code into your project and use it like RequiredEmptyIf("OtherField", "TargetValue").
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store