The JavaScript length character length function

Source: Internet
Author: User

The JavaScript length character length function, we use the length function for a lot of time, because you have to have the front desk judge whether a user's username and password length is what I want to specify.

Advanced scripters often need to know how long a JavaScript string is. For example, if a webdeveloper is to create a submit form that requires the user to no longer exceed 20 characters, then she will need to check the length of the string before allowing the user to submit the data.


String length

The Length property returns the number of characters in a string, with an integer. The following is the basic code used to access this property.

<script type= "Text/javascript" >
var myString = "123456";

var length = mystring.length;

document.write ("The string is this long:" + length);

Same thing, but using the property inside the Write function
document.write ("<br/>the string is this long:" + mystring.length);

</script>

String Changed? Length might change
if you refer to the length of the property, concatenating (add) certain characters of the string, and then the length of the property will reflect these changes. Think of this as a friendly reminder will only check the length of the string after you are sure it is not going to change the

<script type= "Text/javascript"
var myString = "123456";
document.write ("The string is this long:" + mystring.length );

myString = myString + "7890";
document.write ("<br/>the string is now" long: "+ mystring.length );

</script>

Output 6,10

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.