Javascript


Related applications


Search a word:
   



© The scientific sentence. 2010

Javascript

Javascript: Strings



1. Some methods

We will use length, charAt(), and indexOf predefined methods:

Examples:
<script type="text/javascript">

document.writeln('

Strings

'); var word1 = 'abc'; var word2 = 'def'; var word = word1 + word2; document.writeln('word1 = ' + word1 + '
'); document.writeln('word2 = ' + word2 + '
'); document.writeln('word = word1 + word2 = ' + word + '
'); document.writeln('word.length = ' + word.length + '
'); document.writeln('word.charAt(1) = ' + word.charAt(1) + '
'); document.writeln('word.indexOf(\'d\') = ' + word.indexOf('d') + '
'); </script> will output:
Strings word1 = abc word2 = def word = word1 + word2 = abcdef word.length = 6 word.charAt(1) = b word.indexOf('d') = 3

2. Regular expression: Example


Canadian postal code:

<script type="text/javascript">

function check_form() {
var pattern = /^[A-Z][0-9][A-Z] ?[0-9][A-Z][0-9]$/;
if (pattern.test(document.formPC.pc.value)) {
return true;
} else {
window.alert(" The entered postal code is not valid .. ");
return false;
}
</script>

<h3>Check Form</h3>
<form name="formPC" action="index.html" method="post"
onSubmit="return check_form()">
<table>
<tr>
<td>Postal code</td>
<td><input type="text" name="pc" value="" size="7" maxlength="7"></td>
</tr>
<tr><td colspan="2" align="center"><input type="submit"></td></tr>
</table>
</form>
The canadian code takes normally the form: "A1B 2C3".

Check the form

Postal code
  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.