1

i m looking for code in which for not allowed only blank space... for e.g i have one textbox and i have tried this

$(document).ready(function()
{  
   $("#mybutton").live('click',function() 
   {
     var txt_family_name=$("#mytextbox").val();
     if(txt_family_name =="" || txt_family_name ==null)
     {
       alert("null"); 
     }
     else
     {
       alert("not null");
     }
   });
});

this above code i have tried and its not working. so pls help me on that.. on one of my button i m calling this above code

Example : space....with any text -- output should be not null : space space.... any space without any other text -- output should be null

3
  • Do you at least get one of the alerts shown? Commented Apr 9, 2015 at 11:54
  • suppose i m entering only blank space than i m getting "not null" alert Commented Apr 9, 2015 at 11:59
  • Please do not use the jquery-validate tag when you're not using the jQuery Validate plugin. Edited. Thanks. Commented Apr 9, 2015 at 14:54

5 Answers 5

2
you can use the length attribute and the trim method to remove the trailing spaces, if any: 

$("#mybutton").on('click',function() 
   {
     var length = $.trim($("#mytextbox").val()).length;
     if(length == 0)
     {
       alert("null"); 
     }
     else
     {
       alert("not null");
     }
   });
Sign up to request clarification or add additional context in comments.

Comments

1

See the updated code it's working

$(document).ready(function()
{  
$("#clickme").on('click',function() 
{
  var txt_family_name=$.trim($("#mytextbox").val());
 if(txt_family_name ==="" || txt_family_name ===null)
 {
   alert("null"); 
 }
 else
 {
   alert("not null");
 }
});
});

Comments

1

Jquery Validation : require method only check the length of the input. So it allow the blank space.The solution will be the simple change the one line code in it.

required: function( value, element, param ) {

        // Check if dependency is met
        if ( !this.depend( param, element ) ) {
            return "dependency-mismatch";
        }
        if ( element.nodeName.toLowerCase() === "select" ) {

            // Could be an array for select-multiple or a string, both are fine this way
            var val = $( element ).val();
            return val && val.length > 0;
        }
        if ( this.checkable( element ) ) {
            return this.getLength( value, element ) > 0;
        }
        return value.length > 0;
    }

in above code change value.length to $.trim(value).length so simply remove the blank space

Comments

0

you can use regexp.

$(document).ready(function() {
  $("#mybutton").bind('click', function() {
    var txt_family_name = $("#mytextbox").val();
    if (txt_family_name.replace(/\s/g, '') == "") {
      alert("null");
    } else {
      alert("not null");
    }
  });
});

Comments

0

//To add method to remove blankspaces

$.validator.addMethod("blankSpace", function(value) { 
  return value.indexOf(" ") < 0 && value != ""; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.