简体   繁体   中英

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

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)

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, '');
  };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM