I'd like to add validation to a <select> list. It has two values, which should not be accepted.
The first one is the default text saying "Please select something!", it's value is 0. The second one is an option to add a thing that is not on the list. The text it "Other:", it's value is 21.
So basically I'd like to add validation that validates my select list, if value 0 is returned it should return false.
Also, if the user selects the "other:" (value 21) option, it shows a text input with jQuery. How can I validate on
server side that if that text input's css (display) is "inline-block" it takes its value, if it's "none" then it reports that input must be put in that field if the user has selected the "other:" option?
The "SpecifyCodeSnippetLanguage" text input is is the one that is dinamically shown.
About that I thought that i should make a bool variable, if that's true, than handle the validation for it. But how can I check if it's visible or not? Like this:
bool Specify = false;
if (Specify){
Validation.RequireField("SpecifyCodeSnippetLanguage", "Meg kell adnod a programozási nyelvet.");
//code
}
This actually will be a server-side. You have the values of each select option. That means all you need is an IF ELSE block. Now try this
if(select_option == "0") {
<p>Error you have not selected any option.</p>
// And than redirect him back to the page.
} else if (select_option == "2") {// I will assume that 2 is the option for some thing you need to be choosed
<p>You have selected to do this.</p> // Or what ever you need to show.
}
And now the main thing. As far as your server side code is expected use this
var select = "";
select = Request.Form["select_Choice"];
if(select == "0") {
Responce.Redirec(""); // Redirect him to back page. And show him error.
}
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
Marked as answer by trisztann on Jan 24, 2013 04:28 PM
trisztann
Member
14 Points
20 Posts
Validating a <select> list
Jan 23, 2013 11:00 AM|LINK
Hello,
I'd like to add validation to a <select> list. It has two values, which should not be accepted.
The first one is the default text saying "Please select something!", it's value is 0. The second one is an option to add a thing that is not on the list. The text it "Other:", it's value is 21.
So basically I'd like to add validation that validates my select list, if value 0 is returned it should return false.
Also, if the user selects the "other:" (value 21) option, it shows a text input with jQuery. How can I validate on server side that if that text input's css (display) is "inline-block" it takes its value, if it's "none" then it reports that input must be put in that field if the user has selected the "other:" option?
here is my code :
<select name="CodeSnippetLanguage" required="required"> <option selected="selected" value="0">Válassz egy programozási nyelvet!</option> <option value="1">HTML/HAML</option> <option value="2">CSS/SASS</option> <option value="3">Javascript</option> <option value="4">Javascript Könyvtárak (jQuery, stb..)</option> <option value="5">Ruby on Rails</option> <option value="6">PHP</option> <option value="7">ASP</option> <option value="8">ASP.Net (Razor)</option> <option value="9">ASP.Net</option> <option value="10">Yii Framework</option> <option value="11">CakePHP Framework</option> <option value="12">Zend Framework</option> <option value="13">C</option> <option value="14">C++</option> <option value="15">C#</option> <option value="16">Objective C</option> <option value="17">F#</option> <option value="18">Visual Basic</option> <option value="19">Java</option> <option value="20">Ruby</option> <option value="21">Egyéb:</option> </select> <input type="text" name="SpecifiCodeSnippetLanguage" id="SpecifyCodeSnippetLanguage" placeholder="Kódrészlet nyelve"/> @Html.ValidationMessage("CodeSnippetLanguage")The "SpecifyCodeSnippetLanguage" text input is is the one that is dinamically shown.
About that I thought that i should make a bool variable, if that's true, than handle the validation for it. But how can I check if it's visible or not?
Like this:
bool Specify = false; if (Specify){ Validation.RequireField("SpecifyCodeSnippetLanguage", "Meg kell adnod a programozási nyelvet."); //code }Thanks!
Afzaal.Ahmad...
Contributor
2759 Points
1060 Posts
Re: Validating a <select> list
Jan 23, 2013 04:20 PM|LINK
This actually will be a server-side. You have the values of each select option. That means all you need is an IF ELSE block. Now try this
if(select_option == "0") { <p>Error you have not selected any option.</p> // And than redirect him back to the page. } else if (select_option == "2") {// I will assume that 2 is the option for some thing you need to be choosed <p>You have selected to do this.</p> // Or what ever you need to show. }And now the main thing. As far as your server side code is expected use this
var select = ""; select = Request.Form["select_Choice"]; if(select == "0") { Responce.Redirec(""); // Redirect him to back page. And show him error. }~~! FIREWALL !~~
saeed_saedva...
Member
408 Points
74 Posts
Re: Validating a <select> list
Jan 23, 2013 06:42 PM|LINK
Change those values to "" and it will work like below:
<select name="theName"> <option value="">please select</option> <option value="1">first</option> <option value="2">second</option> <option value="">other</option> </select>Saeed Saedvand
asp.netforum...
Member
604 Points
132 Posts
Re: Validating a <select> list
Jan 23, 2013 06:51 PM|LINK
Try this way
http://www.intstrings.com/ramivemula/articles/dropdownlist-validation-in-asp-net-mvc-3razor/
trisztann
Member
14 Points
20 Posts
Re: Validating a <select> list
Jan 24, 2013 04:29 PM|LINK
Thank you!