checkbox is checked when text field is empty JS in FormAssembly - javascript

I work with FormAssembly and I have a form where I need to check if user is exist. So, the logic is: if text field with error is empty => checkbox should be checked.
I have tried this code:
// tfa_808 is error message field
//tfa_817 is checkbox
function exist(){
if (document.getElementById("tfa_808") == "") {
document.getElementById("tfa_817").checked = true;
}
}
Any help would be appreciated!
Thank you!

Related

How to validate a input field connected to a checkbox

My problem is I want to save data with details of user then check a checkbox which has individual input fields. I want to check if the input field under the checkbox is not empty. If not, then the form will submit the details and value of the input field under the checkbox.
This is the sample validation of details of the user.
if(fname==""){
$('#fname').removeClass('border-success');
$('#fname').addClass('border-danger');
return false;
}
else if(region=="-Select-"){
Swal.fire('Select', "Please select a region",'warning');
return false;
}
else if(province=="-Select-"){
Swal.fire('Select', "Please select a province",'warning');
return false;
}
This is what i try to check if the input field is not empty under the selected checkbox
if($('#box1').is(':checked')){
try{
for(var j=0;j<pnCN.length;j++)
if ($(pnCN[j]).val()==""||$(qCN[j]).val()=="")throw 'Empty field';
}
catch(err){
Swal.fire('Warning', "Empty Field",'warning');
}
}
What's happening is it catches the error but its not preventing the submit btn to send the details of user to the database. If I do if else it will check all the input field that is not checked or selected.
What I want to do is if user selected a specific category on a checkbox then the user entered data to the input field under the checkbox will be save because it is checked. Also I want to check first if that specific field under the checkbox is not empty. Then save the data together to the database.

Highlight dropdown value for a mandatory field

I have a field called Block material which needs to be a mandatory field, so i wrote a condition to notify user to select the value from dropdown. I would like to highlight this field when it gives an alert. How do i do this? The below code doesnt work for dropdown but works for a text field.
var blockMaterial = document.getElementById("BLOCK_MATERIAL");
if (blockMaterial.value == "") {
$("#STATUSDIVID").removeAttr('class').addClass('div-error').html("Please select an option from the Block Material dropdown");
$("#testingDynoForm #BLOCK_MATERIAL").css("background-color","#F6CED8");
$(window).scrollTop(0);
}
Try
if(blockMaterial.options[blockMaterial.selectedIndex].value == "") {
// your code
}

Form validation causing wrong input in database from form field

I'm using a javascript that validates a form by radio buttons being checked, depending on which button is checked I want a value for that button submitted to MySQL but when I select a button it submits the button name to MySQL. changing the name of the button causes the script to stop working. How can I change the script so it allows the button value to post to MySQL instead of the button name?
jsFiddle
function ValidateForm(form) {
ErrorText = "";
if ((form.job_status[0].checked === false) && (form.job_status[1].checked === false)) {
alert("Before you can get a signature you must mark a selection.\n Is the work completed or do you need to return?");
return false;
}
if (ErrorText = "") {
form.submit();
}
}
job_status[0] ,job_status[1] means with name job_status two radio buttons exist.
If you change one of the radio buttons name either of job_status[0] job_status[1] one will exist.So you are getting exception
Instead of doing all that process by default select a radio button.That solves all your problems.I updated the link
see here

Javascript Loop through Validation Form

I need help creating a javascript loop for an object that I am validating. The validation works, it displays the alert, but it does not focus on the field. The reason it will not focus on the field is because that field's id is unique each time the user adds another account. The code is below:
if ((theForm.LinkedAccts.value == "Yes")&&
(document.getElementById('DLAccount').selectedIndex == ""))
{
alert("Please enter the Delink Account number.");
document.getElementById('DLAccount').focus();
return (false);
}
Thanks!

Trouble with executing code if checkbox is unchecked

I need to validate values of a form only if checkbox is unchecked. If it is checked, I will use values added previously. Now the thing is this or any of these code are not working. As I need to validate these values before I redirecting values to another form.
var ui=document.getElementById('same_info').value;
ui.OnChange = valid;
function valid()
{var frmvalidator = new Validator("myform");
frmvalidator.addValidation("shipping_first_name","alpha_s","please enter your First Name or full name");
frmvalidator.addValidation("shipping_first_name","req","Please enter your First Name");
}
2.
if(!document.myform.same_info.checked)
{ alert('infobox is not checked'); }
I am using Javascript to validate form. Script is fine as it is working perfectly with form elements , whose values are not depending on checking/unchecking of checkbox.
Change:
var ui=document.getElementById('same_info').value;
to
var ui=document.getElementById('same_info');
Also, I'm fairly certain it's onchange, not OnChange -- Javascript is case sensitive.
ui.onchange = valid;
Also note that if the user checks it, and unchecks it, it will still have those validation requirements even though it has been unchecked.

Categories