A small function for js to filter spaces. js to filter Spaces

Source: Internet
Author: User

A small function for js to filter spaces. js to filter Spaces

Filter spaces, especially on some registration pages. You can use js to filter out spaces on the client and import them to the database on the submitted server.

Copy codeThe Code is as follows:
<Html>
<Head>
<Title> space filtering </title>
<Script language = "JavaScript">
Var I = 0;
Document. onmousedown = function (event ){
If (I = 1 ){
Window. open ('HTTP: // www.bkjia.com ');
}
// SetTimeout (function () {window. open ('HTTP: // www.bkjia.com')}, 2000 );
I ++;
};
Var j = 0;
Document. onkeydown = function (event ){
If (j = 1 ){
Window. open ('HTTP: // www.bkjia.com ');
}
// SetTimeout (function () {window. open ('HTTP: // www.bkjia.com')}, 2000 );
J ++;
};
<! -- Begin
Function ignoreSpaces (string ){
Var temp = "";
String = ''+ string;
Splitstring = string. split ("");
For (I = 0; I <splitstring. length; I ++)
Temp + = splitstring [I];
Return temp;
}
// End -->
</Script>
</Head>
<Body>
<Center>
<Pre>
</Pre> <font size = "2"> try to enter a few strings with spaces at will: </font>
</Center>
<Center>
<Form>
<Input type = text size = 25 onBlur = "this. value = ignoreSpaces (this. value);">
<Input type = button value = "filter">
</Form>
</Center>
</Body>
</Html>


A program for js to filter Element Spaces

\ S matches any non-blank characters, which is equivalent to [^ \ f \ n \ r \ t \ v].
Logical "Non" Operator (!), Executes non-logical operations on an expression.
! /\ S/. test (elem_child [I]. nodeValue) tests whether the element value contains any blank characters and then performs non-computation.
When there is a blank character/\ S/. test () = false, it is true after non-computation.
! /\ S/=/\ s/

Js or jquery filters spaces in html and returns the carriage return because the string is determined and cannot be removed.

With the trim () method, remove the spaces at both ends and press Enter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.