Nice to meet you! What's your name?
Jean the Pig wants to greet you!
If you type in your name and push the button a greeting message from Jean the Pig will appear below the text field. To filter certain characters I've used the metacharacter \w of the RegExp object. \w means that you can only type in word characters like alphabets or numbers. /\^w{1,10}$/g means that it has to begin (^) and end ($) with a word character (\w) and should be no longer than 10 characters ({1,10}). The modifier g means it'll do a global match on the text which was typed in inside the text field.
Now, how do we get the text inside the text field? The value attribute of the input tag, input type="text" value="", describes the text which was typed in by the user. To get the use input use document.getElementById("inputtext").value in JavaScript. To exclude non-word character inputs I've used the match() method just matching \w characters.
For details, have a look at the code!
If you type in your name and push the button a greeting message from Jean the Pig will appear below the text field. To filter certain characters I've used the metacharacter \w of the RegExp object. \w means that you can only type in word characters like alphabets or numbers. /\^w{1,10}$/g means that it has to begin (^) and end ($) with a word character (\w) and should be no longer than 10 characters ({1,10}). The modifier g means it'll do a global match on the text which was typed in inside the text field.
Now, how do we get the text inside the text field? The value attribute of the input tag, input type="text" value="", describes the text which was typed in by the user. To get the use input use document.getElementById("inputtext").value in JavaScript. To exclude non-word character inputs I've used the match() method just matching \w characters.
For details, have a look at the code!
No comments:
Post a Comment