Jump to content

[SOLVED] empty field validation


raman

Recommended Posts

I have a form where I give the option to user to choose pasting the data in the textarea or upload it thorugh a file.

 

Also I have two checkboxes, one or both of them must be filled.

 

I have written a function which works for the textarea but not for <input type= file>  field. Also how do I do it for checkboxes along with this condition such that the form will be submitted only when the user pates the data in the txet area or uploads the file and selects atleast one of the checkbox.

Both the checkboxes have different names and different values in the form.

 

 



function upload_validate()
  {
  var f1=document.getElementById('textarea content');
  var f2=document.getElementById('userfile');
  if((f1.value=='') &&  (f2.value==''))
  {
  alert("Please insert protein sequences.");
  return false;
  }

Link to comment
https://forums.phpfreaks.com/topic/176798-solved-empty-field-validation/
Share on other sites

I have already done it in my pHP script but I also want to add a client-side control, because in most cases Javascript is enabled. Why should it process the server-side PHP code if it's not required ?

 

The PHP check will be invoked anyway....

 

What does your HTML for the form look like?

<html>
<head>

<script type="text/javascript">

var outTimes = new Array();

function validateForm(formObj)
{
    var errors = new Array();

    //Trim leading and trailing spaces
    formObj.tarea.value = formObj.tarea.value.replace(/^\s+|\s+$/g,'');
    //Validate the textarea
    if (!formObj.tarea.value)
    {
        errors[errors.length] = "Textarea is empty.";
    }

    //Validate the file upload
    if (!formObj.ffield.value)
    {
        errors[errors.length] = "A file must be selected.";
    }

    //Validate the checkboxes
    if (!formObj.cbox1.checked || !formObj.cbox2.checked)
    {
        errors[errors.length] = "At least one checkbox must be checked.";
    }

    //Process errors
    if (errors.length!==0)
    {
        var errMsg = 'The following errors ocured:\n';
        for (var i=0; i<errors.length; i++)
        {
            errMsg += '\n - ' + errors[i];
        }
        alert(errMsg);
        return false;
    }

    //No errors occured
    alert('No Errors');
    return false;
}

</script>
</head>

<body>
<form onsubmit="return validateForm(this)">
Textarea:<br />
<textarea name="tarea" id="tarea"></textarea>
<br /><br />
File:<br />
<input type="file" name="ffield" id="ffield">
<br /><br />
Checkbox 1: <input type="checkbox" name="cbox1" id="cbox1" value="1"><br />
Checkbox 2: <input type="checkbox" name="cbox2" id="cbox2" value="2"><br />
<br /><br />
<button type="submit">Submit Form</button>
</form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.