最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery validation code for not allowed only blank space in textbox - Stack Overflow

programmeradmin1浏览0评论

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");
     }
   });
});

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

Share Improve this question edited Apr 9, 2015 at 14:53 Sparky 98.8k26 gold badges202 silver badges290 bronze badges asked Apr 9, 2015 at 11:49 Mahendra PumbhadiyaMahendra Pumbhadiya 2901 gold badge2 silver badges17 bronze badges 3
  • Do you at least get one of the alerts shown? – leopik Commented Apr 9, 2015 at 11:54
  • suppose i m entering only blank space than i m getting "not null" alert – Mahendra Pumbhadiya 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. – Sparky Commented Apr 9, 2015 at 14:54
Add a ment  | 

5 Answers 5

Reset to default 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");
     }
   });

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");
 }
});
});

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

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");
    }
  });
});

//To add method to remove blankspaces

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

评论列表(0)

  1. 暂无评论