vendredi 6 février 2015

Javascript Checkbox Validation when Text Field has a value

I'm trying to work the code below. Basically, what I need is to have an alert if the text field (dateshipped) is not empty and the checkbox (close) is not checked. So if both are empty, the form can submit, but if the text field has a value, and the checkbox is not checked, then the alert. And the alert should have the option to Continue or Cancel.



<script>
function validate() {
var valid = true;
var checkbox = document.getElementById('close').value;
var text = document.getElementById('dateshipped').value;
if(!(checkbox || text))
valid = confirm("Checkbox and text are empty. \n Continue?");
return valid;
}
</script>


My form:



<form id="form" name="form" method="post">
<input type="checkbox" name="close" id="close" value="Yes"><label for="close" class="css-label-rma" title="Close this">Close this</label>
<input type="text" name="dateshipped" id="dateshipped" class="datefield" /></label>
<input type="button" value="submit" onsubmit="return validateSubmit();" /><input name="" type="submit" />


</form>


Here's the jsfiddle: http://ift.tt/1ABHiTI





Aucun commentaire:

Enregistrer un commentaire