Talking about the problem of JS verifying the input space in the form text field

  • 2021-07-21 06:49:21
  • OfStack

Verify that the text field characters entered in the form are spaces, that is, empty strings. Usually, you need to remove the spaces on both sides of the characters to verify accuracy. Otherwise, if you enter more than one space bar continuously, you can't verify it only by document. getElementById ("name"). value = = "".

The method of removing spaces on both sides of strings should also consider the compatibility of browsers.

1. trim () method

document. getElementById ("name"). value. trim () This method has no problem in Chrome and Firefox, and can be executed normally, but an error will be reported in 360 and IE, "The object does not support this property or method".

2. The $. trim () method of jQuery

$.trim(document.loginForm.account.value)

This method can solve the above error of "the object does not support this property or method" in 360 and IE, and it can run normally in Chrome and Firefox.

The above is one small problem encountered in the application implementation, so learn to record one!


Related articles: