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
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?

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.