AngularJs Form validation when select radio buttons












0














Hi i am learner in angularJS and in my form i have name and email fields and one radio group for gender selection and my requirement is when i select male then name field is required and if i select female then email field is required



I have tried below code but its not working can some one help me please



code:



<form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name">Employee Name:</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email">Email Address:</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
autofocus required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid>Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="male" ng-model="empInfo.gender" #gender="ng-model">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="female" ng-model="empInfo.gender" #gender="ng-model">Female
</label>
</div>

</form>









share|improve this question






















  • 1. show log of the gender object when a gender is selected. 2. try changing "required" to "ng-required".
    – holydragon
    Nov 23 at 10:26












  • user = { "name": "zf", "gender": "male" }
    – Krish
    Nov 23 at 10:33










  • please suggest me with code
    – Krish
    Nov 23 at 10:33










  • try changing required="gender.value=='male'" to ng-required="gender.value=='male'"
    – holydragon
    Nov 23 at 10:42










  • no its not working
    – Krish
    Nov 23 at 10:43
















0














Hi i am learner in angularJS and in my form i have name and email fields and one radio group for gender selection and my requirement is when i select male then name field is required and if i select female then email field is required



I have tried below code but its not working can some one help me please



code:



<form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name">Employee Name:</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email">Email Address:</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
autofocus required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid>Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="male" ng-model="empInfo.gender" #gender="ng-model">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="female" ng-model="empInfo.gender" #gender="ng-model">Female
</label>
</div>

</form>









share|improve this question






















  • 1. show log of the gender object when a gender is selected. 2. try changing "required" to "ng-required".
    – holydragon
    Nov 23 at 10:26












  • user = { "name": "zf", "gender": "male" }
    – Krish
    Nov 23 at 10:33










  • please suggest me with code
    – Krish
    Nov 23 at 10:33










  • try changing required="gender.value=='male'" to ng-required="gender.value=='male'"
    – holydragon
    Nov 23 at 10:42










  • no its not working
    – Krish
    Nov 23 at 10:43














0












0








0







Hi i am learner in angularJS and in my form i have name and email fields and one radio group for gender selection and my requirement is when i select male then name field is required and if i select female then email field is required



I have tried below code but its not working can some one help me please



code:



<form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name">Employee Name:</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email">Email Address:</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
autofocus required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid>Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="male" ng-model="empInfo.gender" #gender="ng-model">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="female" ng-model="empInfo.gender" #gender="ng-model">Female
</label>
</div>

</form>









share|improve this question













Hi i am learner in angularJS and in my form i have name and email fields and one radio group for gender selection and my requirement is when i select male then name field is required and if i select female then email field is required



I have tried below code but its not working can some one help me please



code:



<form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name">Employee Name:</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email">Email Address:</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
autofocus required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid>Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="male" ng-model="empInfo.gender" #gender="ng-model">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" value="female" ng-model="empInfo.gender" #gender="ng-model">Female
</label>
</div>

</form>






angularjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 10:08









Krish

1,14662055




1,14662055












  • 1. show log of the gender object when a gender is selected. 2. try changing "required" to "ng-required".
    – holydragon
    Nov 23 at 10:26












  • user = { "name": "zf", "gender": "male" }
    – Krish
    Nov 23 at 10:33










  • please suggest me with code
    – Krish
    Nov 23 at 10:33










  • try changing required="gender.value=='male'" to ng-required="gender.value=='male'"
    – holydragon
    Nov 23 at 10:42










  • no its not working
    – Krish
    Nov 23 at 10:43


















  • 1. show log of the gender object when a gender is selected. 2. try changing "required" to "ng-required".
    – holydragon
    Nov 23 at 10:26












  • user = { "name": "zf", "gender": "male" }
    – Krish
    Nov 23 at 10:33










  • please suggest me with code
    – Krish
    Nov 23 at 10:33










  • try changing required="gender.value=='male'" to ng-required="gender.value=='male'"
    – holydragon
    Nov 23 at 10:42










  • no its not working
    – Krish
    Nov 23 at 10:43
















1. show log of the gender object when a gender is selected. 2. try changing "required" to "ng-required".
– holydragon
Nov 23 at 10:26






1. show log of the gender object when a gender is selected. 2. try changing "required" to "ng-required".
– holydragon
Nov 23 at 10:26














user = { "name": "zf", "gender": "male" }
– Krish
Nov 23 at 10:33




user = { "name": "zf", "gender": "male" }
– Krish
Nov 23 at 10:33












please suggest me with code
– Krish
Nov 23 at 10:33




please suggest me with code
– Krish
Nov 23 at 10:33












try changing required="gender.value=='male'" to ng-required="gender.value=='male'"
– holydragon
Nov 23 at 10:42




try changing required="gender.value=='male'" to ng-required="gender.value=='male'"
– holydragon
Nov 23 at 10:42












no its not working
– Krish
Nov 23 at 10:43




no its not working
– Krish
Nov 23 at 10:43












1 Answer
1






active

oldest

votes


















0














Maybe try with that validation:
HTML :



      <form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name" ng-show="empInfo.gender != male_value">Employee Name is required</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email" ng-show="empInfo.gender == male_value">Email Address is rquired</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid">Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="male_value" ng-model="empInfo.gender">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="female" ng-model="empInfo.gender" >Female
</label>
</div>

</form>


JS:



 $scope.male_value = 'male';


plunker: http://plnkr.co/edit/Hi5t1gU5TIdNx670wHo1?p=preview






share|improve this answer























  • What did i do wrong in my code ,please suggest my code
    – Krish
    Nov 23 at 10:55










  • @Krish What do you have in your JS code?
    – BartoszTermena
    Nov 23 at 10:57












  • i did nothing in js and cant we do this without js code?
    – Krish
    Nov 23 at 10:59










  • please suggest with using radio buttons instead of buttons
    – Krish
    Nov 23 at 11:01










  • any way thanks for your help its helped me
    – Krish
    Nov 23 at 11:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444587%2fangularjs-form-validation-when-select-radio-buttons%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Maybe try with that validation:
HTML :



      <form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name" ng-show="empInfo.gender != male_value">Employee Name is required</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email" ng-show="empInfo.gender == male_value">Email Address is rquired</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid">Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="male_value" ng-model="empInfo.gender">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="female" ng-model="empInfo.gender" >Female
</label>
</div>

</form>


JS:



 $scope.male_value = 'male';


plunker: http://plnkr.co/edit/Hi5t1gU5TIdNx670wHo1?p=preview






share|improve this answer























  • What did i do wrong in my code ,please suggest my code
    – Krish
    Nov 23 at 10:55










  • @Krish What do you have in your JS code?
    – BartoszTermena
    Nov 23 at 10:57












  • i did nothing in js and cant we do this without js code?
    – Krish
    Nov 23 at 10:59










  • please suggest with using radio buttons instead of buttons
    – Krish
    Nov 23 at 11:01










  • any way thanks for your help its helped me
    – Krish
    Nov 23 at 11:30
















0














Maybe try with that validation:
HTML :



      <form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name" ng-show="empInfo.gender != male_value">Employee Name is required</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email" ng-show="empInfo.gender == male_value">Email Address is rquired</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid">Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="male_value" ng-model="empInfo.gender">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="female" ng-model="empInfo.gender" >Female
</label>
</div>

</form>


JS:



 $scope.male_value = 'male';


plunker: http://plnkr.co/edit/Hi5t1gU5TIdNx670wHo1?p=preview






share|improve this answer























  • What did i do wrong in my code ,please suggest my code
    – Krish
    Nov 23 at 10:55










  • @Krish What do you have in your JS code?
    – BartoszTermena
    Nov 23 at 10:57












  • i did nothing in js and cant we do this without js code?
    – Krish
    Nov 23 at 10:59










  • please suggest with using radio buttons instead of buttons
    – Krish
    Nov 23 at 11:01










  • any way thanks for your help its helped me
    – Krish
    Nov 23 at 11:30














0












0








0






Maybe try with that validation:
HTML :



      <form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name" ng-show="empInfo.gender != male_value">Employee Name is required</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email" ng-show="empInfo.gender == male_value">Email Address is rquired</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid">Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="male_value" ng-model="empInfo.gender">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="female" ng-model="empInfo.gender" >Female
</label>
</div>

</form>


JS:



 $scope.male_value = 'male';


plunker: http://plnkr.co/edit/Hi5t1gU5TIdNx670wHo1?p=preview






share|improve this answer














Maybe try with that validation:
HTML :



      <form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

<h3 class="text-center">Insert Employee Details Into Database</h3>

<div class="form-group">
<label for="Name" ng-show="empInfo.gender != male_value">Employee Name is required</label>
<input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
required="gender.value=='male'" />
</div>

<div class="form-group">
<p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
</div>

<div class="form-group">
<label for="Email" ng-show="empInfo.gender == male_value">Email Address is rquired</label>
<input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
required="gender.value=='female'"/>
</div>
<div class="form-group">
<p class="text-danger" ng-show="empList.emp_email.$invalid">Invalid Email!</p>
</div>

<div class="form-group">
<label for="Gender">Gender:</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="male_value" ng-model="empInfo.gender">Male
</label>
<label for="" class="radio-inline gender">
<input type="radio" name="emp_gender" ng-value="female" ng-model="empInfo.gender" >Female
</label>
</div>

</form>


JS:



 $scope.male_value = 'male';


plunker: http://plnkr.co/edit/Hi5t1gU5TIdNx670wHo1?p=preview







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 12:01

























answered Nov 23 at 10:51









BartoszTermena

6661211




6661211












  • What did i do wrong in my code ,please suggest my code
    – Krish
    Nov 23 at 10:55










  • @Krish What do you have in your JS code?
    – BartoszTermena
    Nov 23 at 10:57












  • i did nothing in js and cant we do this without js code?
    – Krish
    Nov 23 at 10:59










  • please suggest with using radio buttons instead of buttons
    – Krish
    Nov 23 at 11:01










  • any way thanks for your help its helped me
    – Krish
    Nov 23 at 11:30


















  • What did i do wrong in my code ,please suggest my code
    – Krish
    Nov 23 at 10:55










  • @Krish What do you have in your JS code?
    – BartoszTermena
    Nov 23 at 10:57












  • i did nothing in js and cant we do this without js code?
    – Krish
    Nov 23 at 10:59










  • please suggest with using radio buttons instead of buttons
    – Krish
    Nov 23 at 11:01










  • any way thanks for your help its helped me
    – Krish
    Nov 23 at 11:30
















What did i do wrong in my code ,please suggest my code
– Krish
Nov 23 at 10:55




What did i do wrong in my code ,please suggest my code
– Krish
Nov 23 at 10:55












@Krish What do you have in your JS code?
– BartoszTermena
Nov 23 at 10:57






@Krish What do you have in your JS code?
– BartoszTermena
Nov 23 at 10:57














i did nothing in js and cant we do this without js code?
– Krish
Nov 23 at 10:59




i did nothing in js and cant we do this without js code?
– Krish
Nov 23 at 10:59












please suggest with using radio buttons instead of buttons
– Krish
Nov 23 at 11:01




please suggest with using radio buttons instead of buttons
– Krish
Nov 23 at 11:01












any way thanks for your help its helped me
– Krish
Nov 23 at 11:30




any way thanks for your help its helped me
– Krish
Nov 23 at 11:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444587%2fangularjs-form-validation-when-select-radio-buttons%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)