Mootools 1.2 tutorial input filtering Part 2 (string)

Source: Internet
Author: User
Tags mootools

In the future, we will introduce the basic knowledge of regular expressions and their usage in MooTools.
Before getting started, I 'd like to take a moment to see how string functions are called. In my example, I directly call this method on the string variable, as shown below:
Reference code:
Copy codeThe Code is as follows:
Var my_text_variable = "Heres some text ";
// Result string variable method name
Var result_of_function = my_text_variable.someStringFunction ();

But I write this only to better understand it. You should understand that these string functions can also be called directly on strings without declaring a variable, just like this:
Reference code:Copy codeThe Code is as follows:
Var result_of_function = "Heres some text". someStringFunction ();

Note that this method is also effective in the number processing functions of MooTools:
Reference code:Copy codeThe Code is as follows:
// Pay attention to the usage, which is a number in parentheses
// Not a string caused by single quotes
Var limited_number = (256). limit (1,100 );

Also, I would like to emphasize that using JavaScript to filter input does not filter data before it is sent to the server. Everything you write in JavaScript can be viewed, manipulated, and disabled by your web page viewers. In the future, we will discuss the MooTools Request class and briefly discuss the PHP filtering technology. At the same time, we should continue to do anything security-related on the server, and do not rely on JavaScript.
Trim ()
The trim function provides a simple and direct way to remove any blank characters at both ends of the string you want to process.
Reference code:
Copy codeThe Code is as follows:
// This is the string we want to trim
Var text_to_trim = "\ nString With Whitespace ";
// The trim String is "String With Whitespace"
Var trimmed_text = text_to_trim.trim ();

If you haven't seen \ n, It's actually just a line break. You can use it in a string to split the string into multiple rows. The trim method also regards the line break as a blank character, so it also removes the line break. The only thing the trim method does not do is: it does not remove any extra spaces in a string. The following example shows how trim processes linefeeds in strings:
Reference code:Copy codeThe Code is as follows:
Var trimDemo = function (){
// Set the string to be trimmed
Var text_to_trim = '\ ntoo much whitespace \ n ';
// Trim the image
Var trimmed_text = text_to_trim.trim ();
// Display the result
Alert ('before Trimming: \ n' +
'|-' + Text_to_trim + '-| \ n \ n' +
'After Trimming: \ n' +
'|-' + Trimmed_text + '-| ');
}


Clean ()
To make it more meaningful, you may not need to remove all spaces in a string. Fortunately, MooTools generously provides you with the clean () method for spaces that you find strong.
Reference code:
Copy codeThe Code is as follows:
// This is the string we want to trim
Var text_to_clean = "\ nString \ nWith Lots \ n More \ nWhitespace \ n ";
// The String after clean is "String With Lots More Whitespace"
Var cleaned_text = text_to_trim.clean ();

The clean () method is a little different from the trim () method. It extracts all the spaces in the characters you pass in, not just the white spaces in the header and tail. They mean any more than one blank character in the string and any line breaks and tabs ). Compare the trim result to see what it means:
Reference code:
Copy codeThe Code is as follows:
Var cleanDemo = function (){
// Set the string to be trimmed
Var text_to_clean = 'too \ n much \ n whitespace ';
// Clean this string
Var cleaned_text = text_to_clean.clean ();
// Display the result
Alert ('before Cleaning: \ n' +
'|-' + Text_to_clean + '-| \ n \ n' +
'After Cleaning: \ n' +
'|-' + Cleaned_text + '-| ');
}


Contains ()
Similar to the trim () and clean () methods, the contains () method is very simple and has no other advantages. It checks a string to see if it contains a string to be searched. If the string to be searched is found, true is returned. If the string is not found, false is returned.
Reference code:
Copy codeThe Code is as follows:
// We need to search for this string
Var string_to_match = "Does this contain thing work? ";
// Find 'containin', and the value of did_string match is true.
Var did_string_match = string_to_match.contains ('containin ');
// Find 'propane '. The value of did_string_match is false.
Did_string_match = string_to_match.contains ('propane ');

This method can be used in various situations when you and other tools, such as the Array. when using the each () function together, you can use a relatively small amount of code to complete some slightly complex tasks. For example, if we put a series of words into an array and traverse them one by one, we can use less code to search for multiple words in the same area of a text:
Reference code:
Copy codeThe Code is as follows:
String_to_match = "string containing whatever words you want to try to match ";
Word_array = ['word', 'to', 'match'];
// Pass every word in the array as a variable
Word_array.each (function (word_to_match ){
// Search for the current word
If (string_to_match.contains (word_to_match )){
Alert ('found '+ word_to_match );
};
});

We put it into a textbox and add an imaginary one, you can have your own dirty word (or any other) detector.
Reference code:
Copy codeThe Code is as follows:
Var containsDemo = function (){
// Put the words we want to disable into an array
Var banned_words = ['drat', 'goshdarn', 'fiddlestick', 'kumquat'];
// Obtain the content in the text field
Var textarea_input = $ ('textarea _ 1'). get ('value ');
// Enumerate each word in a filter word
Banned_words.each (function (banned_word ){
// Search for the current filter word in the text domain content
If (textarea_input.contains (banned_word )){
// Tell the user that the word cannot be used
Alert (banned_word + 'is not allowed ');
};
});
}


Substitute ()
Substitute () is a very powerful tool. Today we will only talk about some basic knowledge about it. More powerful features of substitute come from the use of its regular expressions. We will talk about it later. However, you can do many things only by using these basic functions.
Reference code:
Copy codeThe Code is as follows:
// This is the text template to use the substitute method
// Note that the replacement part is enclosed in curly brackets.
Var text_for_substitute = "One is {one}, Two {two}, Three is {three }.";
// This object contains the rule to be replaced
// Search items are not caused by quotation marks.
// The quotation marks are used to replace the search term sentence.
Var substitution_object = {
One: 'the first variable ',
Two: 'always comes second ',
Three: 'Getting sick of bronze ..'
};
// Call the substitute method on text_for_substitute
// Pass substitution_object as a parameter
// Assign the replacement result to the variable new_string
Var new_string = text_for_substitute.substitute (substitution_object );
// The current value of new_string is "One is the first variable, Two always comes second, Three is getting sick of bronze ..."

In fact, you do not need to create a substitution_object object to use the substitute method. If you think it is inappropriate, the following method can also be implemented:
Reference code:
Copy codeThe Code is as follows:
// Create the string to be replaced
Var text_for_substitute = "{substitute_key} and the original text ";
// Pass the object to be replaced as a parameter to the substitute method
Var result_text = text_for_substitute.substitute ({substitute_key: 'substitute _ value '});
// Result_text is now "substitute_value and the original text"

You can do more with this method. You can use a function call that gets a value from a DOM object to replace the value of an item. This is also possible.
Reference code:
Copy codeThe Code is as follows:
Var substituteDemo = function (){
// Obtain the original text from textfield
Var original_text = $ ('substitute _ span '). get ('html ');
// Replace the value in textfield with the value in the text box
Var new_text = original_text.substitute ({
First: $ ('first _ value'). get ('value '),
Second: $ ('second _ value'). get ('value '),
Third: $ ('third _ value'). get ('value '),
});
// Replace the content in the span with the new text
$ ('Substitute _ span '). set ('html', new_text );
// Disable the substitute button
// And enable the reset button
$ ('Simple _ substitute '). set ('Disabled', true );
$ ('Simple _ sub_reset'). set ('Disabled ', false );
}
Var substituteReset = function (){
// Create a variable to save the original text
Var original_text = "|-{first} -- {second} -- {third}-| ";
// Replace the content in the span with the original text
$ ('Substitute _ span '). set ('html', original_text );
// Disable the reset button
// And enable substitute
$ ('Simple _ sub_reset'). set ('Disabled ', true );
$ ('Simple _ substitute '). set ('Disabled', false );
}

|-{First} -- {second} -- {third}-|
First_value
Second_value
Third_value
Before the end of today, there is a very small prompt. If you call the substitute method on a string and do not provide a key/value pair (key/value pair) for the keyword to be replaced) object, then it will simply delete the content in the curly brackets. Therefore, do not use this method if you need to keep the strings in curly brackets. For example:
Reference code:
Copy codeThe Code is as follows:
("{One} some stuff {two} some more stuff"). substitute ({one: 'substitution text '});

This will return "substitution text some stuff some more stuff ".
Learn more

Download a zip package that contains your initial needs

  • This guy is amazing)
  • JavaScript string function reference
  • MooTools string document

Related Article

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.