180 likes | 312 Views
ECA 225. Applied Online Programming. strings. this keyword. passes a value to a function based solely on the context of where this is used can be used to pass entire objects, such as all values submitted through a form. <form name=‘myForm’ onSubmit=‘process_form( this ) >.
E N D
ECA 225 AppliedOnline Programming strings ECA 225 Applied Interactive Programming
this keyword • passes a value to a function based solely on the context of where this is used • can be used to pass entire objects, such as all values submitted through a form <form name=‘myForm’ onSubmit=‘process_form( this ) > ECA 225 Applied Interactive Programming
this keyword cont . . . function process_form( input ){ var first = input.first_name.value; var last = input.last_name.value; } • compared to function process_form( ){ var first = document.myForm.first_name.value; var last = document.myForm.last_name.value; } ECA 225 Applied Interactive Programming
String( ) object • creating a String( ) object • a var which has been assigned a string has access to all String properties and methods var greeting = new String( “Hello” ); var greeting = “Hello”; ECA 225 Applied Interactive Programming
String( ) properties • to access a String( ) property use dot notation var greeting = “Hello”; alert( greeting.length ); ECA 225 Applied Interactive Programming
String( ) methods ECA 225 Applied Interactive Programming
String( ) methods cont … ECA 225 Applied Interactive Programming
String( ) methods cont … ECA 225 Applied Interactive Programming
String( ) methods cont … • to use a String( )method use dot notation • all methods return a value of some kind • most of the time, what’s returned is some version of the original string • use of String( ) methods do not change the value of the original string • to save what is returned, assign it to a var ECA 225 Applied Interactive Programming
string.toUpperCase( ) • converts all the characters in the string to uppercase var greeting = “Hello”;var result = greeting.toUpperCase( ); // returns HELLO ECA 225 Applied Interactive Programming
string.toLowerCase( ) • converts all the characters in the string to lowercase var greeting = “Hello”;var result = greeting.toLowerCase( ); // returns hello ECA 225 Applied Interactive Programming
string.indexOf( ) • determines if one string is contained by another • index values are zero based • returns the index of the character in the larger string where the smaller string begins • if no match occurs, -1 is returns • to check for a simple match, test whether the returned value is something other than –1 • optional 2nd parameter, the starting index of the search ECA 225 Applied Interactive Programming
string.indexOf( ) cont … var greeting = “Hello my Baby.”; var result = greeting.indexOf(“Hello”);alert( greeting );alert ( result ); • change argument to “llo” • change argument to “ool” ECA 225 Applied Interactive Programming
string.charAt( ) • extracts a single character at a known position • pass the method an index number as an argument • returns the character at the index number var greeting = “Hello my Baby.”; var letter = greeting.charAt( 0 ); alert ( letter ); ECA 225 Applied Interactive Programming
string.charAt( ) cont … • print the string using • for loop • length property • charAt( ) var greeting = “Hello my Baby.”; for( i=0; i<greeting.length; i++ ){ document.write(greeting.charAt( i )) } ECA 225 Applied Interactive Programming
string.substring( ) cont … • extracts a contiguous string of characters • takes 2 parameters • beginning index • ending index ( not part of the substring ) var greeting = “Hello my Baby.”; var result = greeting.substring( 0,4 ); alert( result ); ECA 225 Applied Interactive Programming
string.substr( ) cont … • extracts a contiguous string of characters • takes 2 parameters • beginning index • length of the substring var greeting = “Hello my Baby.”; var result = greeting.substr( 9,4 ); alert( result ); ECA 225 Applied Interactive Programming
form validation • form input validation using charAt( ) function valZip( ){ var zip = document.form1.zip.value; if( zip.length != 5 ) { alert( “Invalid zip code") } for( i=0; i < zip.length; i++) { if( zip.charAt( i ) < 0 || zip.charAt( i ) > 9 ) { alert( “Invalid zip code") } } ECA 225 Applied Interactive Programming