I have this form where I need to validate that - "If the user checks the checkbox, they must enter data in the text field. I have the following JS where I can verify the parent/child checkbox validation, but I am not sure how to use the script for text field validation. Thanks!
$(document).ready(function () {
$('#checkBtn').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
alert("You must check at least one checkbox.");
return false;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="checkbox" class="model"> Model #
<input type="text" size="12" class="childModel"><BR>
<input type="submit" name="submit" value="submit" id="checkBtn" style="text-align:center;"/>
Sounds like you just need to check that:
either the checkbox is unchecked, OR
there's something in the checkbox
Here's a simple way to do it:
$(document).ready(function () {
$('#checkBtn').click(function() {
var isCheckboxChecked = $("input[type=checkbox]:checked").length;
var isTextEntered = $("input.childModel").val().length;
if ( isTextEntered || !isCheckboxChecked ) {
alert("validation passed!");
} else {
alert("validation failed!");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="checkbox" class="model"> Model #
<input type="text" size="12" class="childModel"><BR>
<input type="submit" name="submit" value="submit" id="checkBtn" style="text-align:center;"/>
I have below code to set fullName value, logic works but after it sets the value all the textbox values are cleared. I need overcome this issue.
<script>
$(document).ready(function() {
$('#check').click(function() {
if ( $('#city').val() == '' )
{
alert('Empty!!!');
}
else
{
$('#fullName').val("hi");
}
});
});
</script>
<form name="form1" method="post" action="">
City: <input type="text" name="city" id="city"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
Full name: <input type="text" name="fullName" id="fullName"><br>
Phone number: <input type="text" name="Last" value="Mouse"><br>
<button id="check">Check</button>
</form>
By default a button within a form but with no type is a submit button. My guess is you are actually submitting the form when you click the button, and the inputs are 'cleared' because a new page is loading.
Try adding type="button" to your button.
<button id="check" type="button">Check</button>
Use PreventDefault() to cancel postback, becouse your button does not have type so HTML put the default behavior like submit, try:
$('#check').click(function(e) {
e.preventDefault();
if ( $('#city').val() == '' )
{
alert('Empty!!!');
}
else
{
$('#fullName').val("hi");
}
});
LIVE DEMO
I want to submit the form if and only if the textbox is checked... if not I want to stop the form and give an alert
Right now I am getting the alert if it is not checked but it's submitting the form either way
$(function() {
$("#button").click(function(){
if ($("#uploadTOS").is(":checked")) {
return true;
} else {
alert("Please agree to the TOS");
return false;
}
})
})
<form enctype="multipart/form-data" action="urlupload.php?do=verify" id="form" method="post" onsubmit="a=document.getElementById('form').style;a.display='none';b=document.getElementById('part2').style;b.display='inline';" style="display: inline;">
<div id="uploadform">
<input type="text" name="uploadfile" size="30" /><p>
<input type="submit" id="button" class="button" value="Upload" />
</div>
<div id="TOSDiv" style="display:none; z-index:60;">
<input id="uploadTOS" type="checkbox" name="tos" value="1" />
<span id="uploadTOSText">
I Agree to the Terms of Service
</span>
</div>
</form>
Use onclick javascript with this:
function toscheck() {
if ($("#uploadTOS").is(":checked")) {
return true;
} else {
alert("Please agree to the TOS");
return false;
}
}
Change javascript to:
function submitcheck() {
if ($("#uploadTOS").is(":checked")) {
return true;
} else {
alert("Please agree to the TOS");
return false;
}
}
And then add onclick to the submit button
<input type="submit" id="button" class="button" value="Upload" onclick="return submitcheck();" />
I believe return values are deprecated for newer versions of jQuery. Try event.preventDefault instead of return false.
Note that the event variable would simply by the first argument to your jQuery .click(function(event) {...}) handler.
I usually work with PHP so sadly don't have some basic JS principles down. This is all I want to accomplish--I've seen many posts on this topic but they are usually beyond what I need.
Here is my form:
<input type="checkbox" name="checkbox" value="check" />
<input type="submit" name="email_submit" value="submit" onclick="----??----" />
The checkbox is a simple "I agree". I want the submit button to be pressed and it will only submit if that check box is selected.
Here's the thing: I want the simple, cheating way -- no methods -- just some inline code in that form (assuming its not overly long?). This is not a public page, I just need something quick and simple with that type of validation. If its unchecked, it will throw an alert(); if its checked it will submit via post through php and go on as normal.
You could use:
if(!this.form.checkbox.checked)
{
alert('You must agree to the terms first.');
return false;
}
(demo page).
<input type="checkbox" name="checkbox" value="check" />
<input type="submit" name="email_submit" value="submit" onclick="if(!this.form.checkbox.checked){alert('You must agree to the terms first.');return false}" />
Returning false from an inline event handler will prevent the default action from taking place (in this case, submitting the form).
! is the Boolean NOT operator.
this is the submit button because it is the element the event handler is attached to.
.form is the form the submit button is in.
.checkbox is the control named "checkbox" in that form.
.checked is true if the checkbox is checked and false if the checkbox is unchecked.
For now no jquery or php needed. Use just "required" HTML5 input attrbute like here
<form>
<p>
<input class="form-control" type="text" name="email" />
<input type="submit" value="ok" class="btn btn-success" name="submit" />
<input type="hidden" name="action" value="0" />
</p>
<p><input type="checkbox" required name="terms">I have read and accept SOMETHING Terms and Conditions</p>
</form>
This will validate and prevent any submit before checkbox is opt in. Language independent solution because its generated by users web browser.
You can do something like this:
<form action="../" onsubmit="return checkCheckBoxes(this);">
<p><input type="CHECKBOX" name="MyCheckbox" value="This..."> This...</p>
<p><input type="SUBMIT" value="Submit!"></p>
</form>
<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(theForm) {
if (
theForm.MyCheckbox.checked == false)
{
alert ('You didn\'t choose any of the checkboxes!');
return false;
} else {
return true;
}
}
//-->
</script>
http://lab.artlung.com/validate-checkbox/
Although less legible imho, this can be done without a separate function definition like this:
<form action="../" onsubmit="if (this.MyCheckbox.checked == false) { alert ('You didn\'t choose any of the checkboxes!'); return false; } else { return true; }">
<p><input type="CHECKBOX" name="MyCheckbox" value="This..."> This...</p>
<p><input type="SUBMIT" value="Submit!"></p>
</form>
You can do the following:
<form action="/" onsubmit="if(document.getElementById('agree').checked) { return true; } else { alert('please agree'); return false; }">
<input type="checkbox" name="checkbox" value="check" id="agree" />
<input type="submit" name="email_submit" value="submit" />
</form>
Here is a working demo - http://jsfiddle.net/Ccr2x/
If your checkbox has an ID of 'checkbox':
if(document.getElementById('checkbox').checked == true){ // code here }
HTH
var confirm=document.getElementById("confirm").value;
if((confirm.checked==false)
{
alert("plz check the checkbox field");
document.getElementbyId("confirm").focus();
return false;
}
If the check box's ID "Delete" then for the "onclick" event of the submit button the javascript function can be as follows:
html:
<input type="checkbox" name="Delete" value="Delete" id="Delete"></td>
<input type="button" value="Delete" name="delBtn" id="delBtn" onclick="deleteData()">
script:
<script type="text/Javascript">
function deleteData() {
if(!document.getElementById('Delete').checked){
alert('Checkbox not checked');
return false;
}
</script>
Another simple way is to create a function and check if the checkbox(es) are checked or not, and disable a button that way using jQuery.
HTML:
<input type="checkbox" id="myCheckbox" />
<input type="submit" id="myButton" />
JavaScript:
var alterDisabledState = function () {
var isMyCheckboxChecked = $('#myCheckbox').is(':checked');
if (isMyCheckboxChecked) {
$('myButton').removeAttr("disabled");
}
else {
$('myButton').attr("disabled", "disabled");
}
}
Now you have a button that is disabled until they select the checkbox, and now you have a better user experience. I would make sure that you still do the server side validation though.
Another Simple way is to create & invoke the function validate() when the form loads & when submit button is clicked.
By using checked property we check whether the checkbox is selected or not.
cbox[0] has an index 0 which is used to access the first value (i.e Male) with name="gender"
You can do the following:
function validate() {
var cbox = document.forms["myForm"]["gender"];
if (
cbox[0].checked == false &&
cbox[1].checked == false &&
cbox[2].checked == false
) {
alert("Please Select Gender");
return false;
} else {
alert("Successfully Submited");
return true;
}
}
<form onload="return validate()" name="myForm">
<input type="checkbox" name="gender" value="male"> Male
<input type="checkbox" name="gender" value="female"> Female
<input type="checkbox" name="gender" value="other"> Other <br>
<input type="submit" name="submit" value="Submit" onclick="validate()">
</form>
Demo: CodePen
Target it by id and then use this code:
function check(){
if(document.getElementById('yourid').checked
{
return false;
}
else
{
alert ("checkbox not checked");
return false;
}
}
var testCheckbox = document.getElementById("checkbox");
if (!testCheckbox.checked) {
alert("Error Message!!");
}
else {
alert("Success Message!!");
}
Guys you can do this kind of validation very easily. Just you have to track the id or name of the checkboxes. you can do it statically or dynamically.
For statically you can use hard coded id of the checkboxes and for dynamically you can use the name of the field as an array and create a loop.
Please check the below link. You will get my point very easily.
http://expertsdiscussion.com/checkbox-validation-using-javascript-t29.html
Thanks
My Form looks like this:
<body>
<form id="myForm" method="post">
<label>id:</label>
<input type="text" name="id" id="id" size="50"/>
<div id="hidden" style="display: none;">
<label>Name:</label>
<input type="text" name="name" size="50"/><br/>
<input type="button" id="button2" value="Update" size="25" /> <br/>
</div>
<input type="button" id="button1" value ="Get Info" onclick="document.getElementById('hidden').style.display = ''; this.style.display = 'none'" size="25"/>
I have JS that looks like this
$(document).ready(function(){
$("#button1").click(function(){
$.post(
'xxx.php',
{ id: $('input[name="id"]', '#myForm').val() },
function(json) {
if (json.abc === 'no'){
alert('does not exist');
}
else{
$("input[name='name']").val(json.name);
}},
"json"
);
});
$("#button2").click(function(){
$('form#myForm').attr({action: "xxx1.php"});
$('form#myForm').submit();
});
});
The problem is that the user can only submit this form by clicking on the submit button. Any ideas on how i can adjust the js so that the enter button(on keyboard) also submits the form?
Note: there are two submit buttons both are interlinked.
You could give your input element an id, for easier retrieval:
<input id="txtName" type="text" name="name" size="50"/><br/>
Then you may bind your function to the keypress event:
$('#txtName').keypress(function (e) {
if (e.which == 13) {
$("#button1").click()
}
});
Or, for a general case, you may want to just bind the function to every text box of the form:
$('#myForm input:text').keypress(function (e) {
if (e.which == 13) {
$("#button1").click()
}
});
This code works for me.
$('#yourform').bind('submit', function() {