Greetings everyone.
The Newbie is coming back from uni...
I knew making a good form validation JavaScript would be a piece of cake for you guys, but as a novice programmer as me, I still find some difficulties to make my code perfect.
Yeah, I made it, by the help of the generous Google, it goes like this:
------------------------------My code Starts-------------------------------
[html]
<html>
<head>
<title>the test page for form validation</title>
</head>
<body>
<!-- This template is taken from:http://www.plus2net.com/javascript_tutorial/javascript_validation.php -->
<p><h1>The purpose of the first checking is to see if user input wa just a 8-digit number.</h1></p>
<script type='text/javascript'>
function mediacodeValidate(elem, helperMsg){
<!-- the regular expression is for check whether the user input fits the rule:"a string made up with exactly 8 numerical letters", or better to say, "a number with exactly 8 digits".
-->
var numericExpression = /^[0-9]{8}/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<form>
Please inter a 8-digit number for the mediacode: <input type='text' id='numbers' maxlength="8" onblur="mediacodeValidate(document.getElementById( 'numbers'), 'Please inter exactly a 8-digit number!')"/>
<input type='button'
onclick="mediacodeValidate(document.getElementById ('numbers'), 'Please inter exactly a 8-digit number!')"
value='Check Field' />
</form>
</body>
</html>
[/html]
-----------------------------My Code Ends------------------------------
Feel free to take it, it works, and fits the requirement, but...
I cannot make it "outsourced", say, I tried to use <script src="blahblah.js" />, which is referencing the script part in the html head, but I failed...
What was worse, I cannot remove the maxlength constrain of the text.
The reason is, the regular expression would accept any number that are made of no less than 8 digits!
I know the string's beginning sign "^", but cannot find its spouse, which is the ending sign...
And since I just search from the web plus with a discount OLD reference book, called "Pure JavaScript" by Wyke, Gilliam, Ting and Michaels, I wonder if this style is still the preferred way of JavaScript programming.
I must think in advance, plan ahead. Because in late September I will be asked to apply more strict checks against the user inputs. So better think about it now.
Many thanks in advance!
Sincerely yours,
mattmao