el mashrafi el mashrafi - 1 year ago 88
HTML Question

Validate jquery form how to not allow spaces in Full Name

var form = $("#contact_form");
var FName = $("#fname"); //Creates Variables from forms
var FNameInfo = $("#fnameInfo");
var Tele = $("#tele");
var TeleInfo = $("#teleInfo");
var Address = $("#address");
var AddressInfo = $("#addInfo");
var Email = $("#email");
var EmailInfo = $("#EmailInfo");
var regexp = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_.-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9.]+.[a-z]{2,4}$/; //ragex code

FName.blur(validateFName); //blur is a method which attaches a functiom to run when an event occurs

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

rules: {
name: {
noSpace: true

if (validateFName() & validateTele() & validateEmail() & validateAddress()) { //If it passes all of these then return true
return true;
} else {
return false;

function validateFName() { //Creates function for Name Validation+
if (FName.val().length < 5) { //If name is length is less than 5
FName.addClass("error"); //Display error
FNameInfo.text("Please Enter Correct Name"); //Changes span name
FNameInfo.addClass("error"); //Same error
return false;
else {
FName.removeClass("error"); //remove error class
FNameInfo.text("Thanks Buddy" ); //When user enters correct code this text will appear
return true;

<label for="name">Full Name</label>
<input id="fname" name="fname" type="text" />
<span id="fnameInfo">Whats your full name?</span>

How do I get the validation to work for Full Name when user enters blank spaces? I have the code above but it doesn't seem to do anything. Would I need to change the function parameters?

Answer Source

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

and the name for full name is fname.


    rules: {
       fname: {
         noSpace: true

should work.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download