Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a Form With Multiple Values For A Common Field "service". I have created an option "Other" For selection. And I want to validate with javascript input field next to "other" option. Code is as follows

C#
<label for="service" style="font-size:15px; vertical-align:top;">Service You Want*</label>

<input type="checkbox" name="service[]" id="Complete new set up " value="Complete new set up ">Complete new set up<br>
<input type="checkbox" name="service[]" id="Standardization" value="Standardization ">Standardization<br>
<input type="checkbox" name="service[]" id="Training" value="Training">Training <br>
<input type="checkbox" name="service[]" id="Trouble shooting " value="Trouble shooting ">Trouble shooting <br>
<input type="checkbox" name="service[]" id="Addition of new techniques" value="Addition of new techniques">Addition of new techniques<br>
<input type="checkbox" name="service[]" id="Hands on training" value="Hands on training">Hands on training<br>
<input type="checkbox" name="service[]" id="Seminars & Workshops" value="Seminars & Workshops">Seminars & Workshops<br>
<input type="checkbox" name="service[]" id="Preparations of documents" value="Preparations of documents ">Preparations of documents <br>
<input type="checkbox" name="service[]" id="Other"  önclick="enable_text(this.checked)"  value="other" >Other

<input type="text" name="other_text"><br>

For "Other" option, Input Text Field get Enabled if "other" options is checked.
working javascript used to enable input field is :
C#
<script>
    function enable_text(status)
    {
        status=!status;
        document.formname.other_text.disabled = status;
    }
</script>

javascript used To select atleast one service is working and it is as follows:
C#
<script>
    var chks = document.getElementsByName(&amp;#39;service[]&amp;#39;);
    var hasChecked = false;
    for (var i = 0; i &amp;lt; chks.length; i++)
    {
        if (chks[i].checked)
        {
            hasChecked = true;
            break;
        }
    }
    if (hasChecked == false)
    {
        alert(&amp;quot;Please Select At Least One Service.&amp;quot;);
        return false;
    }
</script>



But I am not getting how to validate input field if "Other" option from checkboxes is Checked.
Posted
Updated 25-Jun-14 19:42pm
v5

HTML
HTML
<input type="checkbox" name="others"  önclick="enable_text(this.checked)" />
<input type="text" id="other_text" />

JavaScript
JavaScript
function enable_text(status)
{
  document.getElementById("other_text").disabled = !status;
}

DEMO[^]

EDIT
JavaScript
if (document.getElementById("Other").checked && isBlank(document.getElementsByName('other_text').value)) {
    alert("your message......... ");
    return false;
}

use below function to check blank input
JavaScript
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
 
Share this answer
 
v2
Comments
Manish Joshi 26-Jun-14 1:32am    
No no...I am asking how to validate that this input field is not Empty if "other" is checked / selected. how to alert if user slected "Other'Option Byt input field next to it kept empty...?
Manish Joshi 26-Jun-14 2:26am    
yes...sir, your edited code worked for me...Only I added one more line in code to focus other_text field...
if (document.getElementById("Other").checked && isBlank(document.getElementsByName('other_text').value)) {
alert("your message......... ");
document.contactus.other_text.focus();
return false;
Manish Joshi 26-Jun-14 2:26am    
thank you very much
Manish Joshi 26-Jun-14 2:48am    
Resp Sir, Even After filling some text in "Other_text" field, It is Again n again Alerting..."your message....."
<html>

<head>

</head>
<body>
<label for="service" style="font-size:15px; vertical-align:top;">Service You Want*</label>

<input type="checkbox" name="service[]" id="Complete new set up " value="Complete new set up ">Complete new set up<br>
<input type="checkbox" name="service[]" id="Standardization" value="Standardization ">Standardization<br>
<input type="checkbox" name="service[]" id="Training" value="Training">Training <br>
<input type="checkbox" name="service[]" id="Trouble shooting " value="Trouble shooting ">Trouble shooting <br>
<input type="checkbox" name="service[]" id="Addition of new techniques" value="Addition of new techniques">Addition of new techniques<br>
<input type="checkbox" name="service[]" id="Hands on training" value="Hands on training">Hands on training<br>
<input type="checkbox" name="service[]" id="Seminars & Workshops" value="Seminars & Workshops">Seminars & Workshops<br>
<input type="checkbox" name="service[]" id="Preparations of documents" value="Preparations of documents ">Preparations of documents <br>
<input type="checkbox" id="other" name="other" id="Other" value="other" >Other

<input type="text" id="other_text" name="other_text"><br>


<input type="button" value="Click Here" onclick="javascript:return validateForm()">
</body>
<html>

<script>
function validateForm( )
{
var chks = document.getElementsByName('service[]');
var other = document.getElementsByName('other');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if((!hasChecked)&& !(document.getElementById('other').checked))
{
alert("Please chaek any of one");
} else if(document.getElementById('other').checked)
{

if (document.getElementById('other_text').value == '') {
alert("Enter the other details ");
document.getElementById('other_text').focus();
return false;
}

}

return true;
}
</script>






Hai,


Try this Code
 
Share this answer
 
Comments
Ashok8033 26-Jun-14 1:43am    
Hai use this code if any clarification ask me
Manish Joshi 26-Jun-14 1:48am    
yes, thank you, i will try and will give reply to you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900