Hildy Maass
Hildy Maass

Reputation: 97

trim() not working for input field validation

I used this inputNameVal.trim(); inside a click (submit) function

it doesn't work by using

if(name !=""){
//passed
}

the user can put insert blank data

Upvotes: 0

Views: 1285

Answers (2)

John Boker
John Boker

Reputation: 83729

jQuery has a trim function that can be used like:

var trimmedValue = $.trim(inputNameVal);

Also, inputNameVal.trim() is a function call that does not modify the value of the string, you'd have to have something like:

inputNameVal = inputNameVal.trim()

if String.trim is not defined for your browser mozilla documentation says you can define it yourself like:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, '');
  };
}

Upvotes: 1

Shikiryu
Shikiryu

Reputation: 10219

String.trim() is not known by all browsers (see at the bottom of the page).

As you tagged this question with jQuery, you can use $.trim() like this :

$.trim(inputNameVal)

Upvotes: 1

Related Questions