Jquery Validation Code for Not Allowed Only Blank Space in Textbox

Jquery validation code for not allowed only blank space in textbox

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

Validate jquery form how to not allow spaces in Full Name

"contact_form" needs an # for id or . for class.

and the name for full name is fname.

so:-

$("#contact_form").validate({
rules: {
fname: {
noSpace: true
}
}
});

should work.

FIDDLE

Not allow space as a first character and allow only letters using jquery

Can you use the HTML5 attribute pattern? See the MDN article on it for more information.

Using a regex of ^[a-zA-Z][\sa-zA-Z]* seems to cover your requirements.

So something like:

<div>Username:</div><input type="text" pattern="^[a-zA-Z][\sa-zA-Z]*" title="Can use upper and lower letters, and spaces but must not start with a space" />

jquery form validate not allow space for username field?

you can try something like this:

$(document).ready(function(){

jQuery.validator.addMethod("noSpace", function(value, element) {
return value.indexOf(" ") < 0 && value != "";
}, "No space please and don't leave it empty");


$("form").validate({
rules: {
name: {
noSpace: true
}
}
});


})

quick demo

more on addMethod here

and an example also here.

how to prevent using space while typing?

To prevent a space in your input element, you could do this using jQuery:

Example: http://jsfiddle.net/AQxhT/

​$('input').keypress(function( e ) {
if(e.which === 32)
return false;
})​​​​​;​

.

$('input').keypress(function( e ) {    
if(!/[0-9a-zA-Z-]/.test(String.fromCharCode(e.which)))
return false;
});​

Not allow a blank character / space in a input form

Check this Fiddle. Relevant code:

 $(function() {        $('#input1').on('keypress', function(e) {            if (e.which == 32){                console.log('Space Detected');                return false;            }        });});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="input1" />

JQuery validation accepts even if input contains only whitespaces

Just add this method before your jQuery validate function run::

jQuery.validator.addMethod("noSpace", function(value, element) { 
return value.indexOf(" ") < 0 && value != "";
}, "No space please and don't leave it empty");

AND use

noSpace: true

for each field you want.

Allow space in textbox only if any existing characters or number in that

You can do it like this:

$(function() {
$('#input1').on('keypress', function(e) {
if ($(this).val() == "")
if (e.which == 32)
return false;
});
});

Online Demo (jsFiddle)

Jquery Validation: allow only alphabets and spaces

Instead of the below regex:

/^[a-zA-Z]+$/

Use this:

/^[a-zA-Z\s]+$/

This will also take the space.



Related Topics



Leave a reply



Submit