My status check is not working properly in JS

I am trying to show a warning message when the checkbox is not selected. For this, I use the following code

function IsEmpty(){
        var oldpath = document.forms['pathuploader'].oldpath.value;
        var newpath = document.forms['pathuploader'].newpath.value;
        var metavalue = !document.forms['pathuploader'].chkmeta.checked;
        var postvalue = !document.forms['pathuploader'].chkpost.checked;
      if((oldpath == "")||((oldpath.substring(0,4))!='http')||((oldpath.substring(0,4))=='Http'))
        {
            alert("Enter a valid URL");
            return false;

        }
        if((newpath == "")||(newpath.substring(0,4)!='http')||(newath.substring(0,4)!='Http'))
        {
            alert("Enter a valid URL");
            return false;

        }
        if((metavalue) && (postvalue))
        {
            alert("Select any category to change");
            return false;
        }
        return true;
}
+4
source share
3 answers

JSFiddle Work

First of all, you have a typo on the next line

if((newpath == "")||(newpath.substring(0,4)!='http')||(newath.substring(0,4)!='Http'))

Last, if "newath" should be "newpath", and the same area "! =" Should match the logic of oldpath and instead be "==".

To clear the code a bit more, use "===" and "! ==" instead of "==", as this provides a more accurate comparison.

See this link for more information. Use strict mode.

Here is the adjusted code

, camelCase, JS. "IsEmpty" "isEmpty" .

function isEmpty(){
   var oldpath = document.forms['pathuploader'].oldpath.value;
   var newpath = document.forms['pathuploader'].newpath.value;
   var metavalue = !document.forms['pathuploader'].chkmeta.checked;
   var postvalue = !document.forms['pathuploader'].chkpost.checked;
  if((oldpath === "")||((oldpath.substring(0,4))!=='http')||((oldpath.substring(0,4))==='Http'))
   {
    alert("Enter a valid old URL");
    return false;

   }
  if((newpath === "")||(newpath.substring(0,4)!=='http')||(newpath.substring(0,4)==='Http')){
    alert("Enter a valid new URL");
    return false;
  }
  if((metavalue) && (postvalue)){
    alert("Select any category to change");
    return false;
  }
  return true;

}

"Sourabh", BANG (!).

if(( !metavalue ) && ( !postvalue ){ 

, . , BANG . , , , , ,

var metaValueNotChecked = !document.forms...
var postValueNotChecked = !document.forms...

if(( metaValueNotChecked ) && ( postValueNotChecked ){ 

BANG , .

, !

+1

, , , ,

:

var metavalue = document.forms['pathuploader'].chkmeta.checked;
var postvalue = document.forms['pathuploader'].chkpost.checked;

if :

if(!metavalue && !postvalue)
        {
            alert("Select any category to change");
            return false;
        }
0

You can use the “required” from HTML5 and remove it, as soon as the checkbox is selected, from all the rest of your checkboxes. ex:

<input required="required" value="1" name="site[source][]" id="site_source_any" type="checkbox">

<input required="required" value="2" name="site[source][]" id="site_source_many" type="checkbox">

In your script file:

<script type="text/javascript">
        // Check if atleast one of the checkbox is checked
        $(function(){
          var requiredCheckboxes = $(':checkbox[required]');
          requiredCheckboxes.change(function(){
          if(requiredCheckboxes.is(':checked')) {
        // Remove Required once at-least one is checked
          requiredCheckboxes.removeAttr('required');
          }
          else {
            requiredCheckboxes.attr('required', 'required');
           }
         });
       });

        </script>
0
source

All Articles