mag wa
01-30-2005, 07:06 PM
This code is to create a web page containing a form that should contain the following input fields: full name (text) and date of birth (text field with input required in the form (DD/MM/YYYY), I want the contents of each text field to be checked on a blur event to ensure they are not empty.....
Suitable contents as well should be checked: full name text field contents starting with letter, contain at least on space and end with a letter
Characters 1,2,4,5,7,8,9,10 of the date of birth should be digits...
if contents are ok, should calculate the age of the person..
I wrote the code but not working....would you be kind and have a look for me ? :confused:
Many thanks!!:)
<html>
<head>
<script type="text/javascript">
<!--
function validate()
{
x=document.myForm
Space=x.fname.value.indexOf(" ")
fullname=x.fname.value
DOB=x.date.value
submitOK="True"
// Checking first character of name
if (/[^a-z]/i.test(fullname.charAt(0)))//if first charachter of name is not a letter
{
alert("Not a valid name, first character should be a letter")
submitOK="False"
}
//checking last character of name
LastLetterNumber=fullname.length //to know the positon of last letter of full name
if (/[^a-z]/i.text(fullname.charAt(LastLetterNumber-1)))//if last character of name is not a letter
{
alert("Not a valid name, last character should be a letter")
submitOK="False"
}
//checking for spaces in full name
if (Space==-1)//if no space was found
{
alert("Not a valid name, we need your full name")
submitOK="False"
}
//checking the characters number 1,2,4,5,7,8,9,10 making sure they are digits.
if ((/[^0-9]/i.(date.charAt(0))) || (/[^0-9]/i.(date.charAt(1))) || (/[^0-9]/i.(date.charAt(3))) || (/[^0-9]/i.(date.charAt(4))) || (/[^0-9]/i.(date.charAt(6))) || (/[^0-9]/i.(date.charAt(7)))
||(/[^0-9]/i.(date.charAt(8))) || /[^0-9]/i.(date.charAt(9)))))
{
alert("Not a valid date of birth")
submitOK="False"
}
if (submitOK=="False")
{
return false
}
}//end of function
//-->
</script>
</head>
<body>
<form name="myForm" onsubmit="return validate()"
onBlur=''
if ((fullname=="")||(date==""))
{
alert(Please do not leave a blank field);
x.fname.focus();
}
>
Enter your full Name: <input type="text" name="fname" size="20"><br />
Enter your date of birth: <input type="text" name="date" size="10"><br />
<input type="submit" value="Submit">
</form>
<script>
<!--
//The resulting output after entering the required correct fields
year=date.substring(5,8);
var s=2005-year;
document.writeln("Hello"+fullname+"Did you know that on your next
birthday you will be"+s+"years old?");
//-->
</script>
</body>
</html>
Suitable contents as well should be checked: full name text field contents starting with letter, contain at least on space and end with a letter
Characters 1,2,4,5,7,8,9,10 of the date of birth should be digits...
if contents are ok, should calculate the age of the person..
I wrote the code but not working....would you be kind and have a look for me ? :confused:
Many thanks!!:)
<html>
<head>
<script type="text/javascript">
<!--
function validate()
{
x=document.myForm
Space=x.fname.value.indexOf(" ")
fullname=x.fname.value
DOB=x.date.value
submitOK="True"
// Checking first character of name
if (/[^a-z]/i.test(fullname.charAt(0)))//if first charachter of name is not a letter
{
alert("Not a valid name, first character should be a letter")
submitOK="False"
}
//checking last character of name
LastLetterNumber=fullname.length //to know the positon of last letter of full name
if (/[^a-z]/i.text(fullname.charAt(LastLetterNumber-1)))//if last character of name is not a letter
{
alert("Not a valid name, last character should be a letter")
submitOK="False"
}
//checking for spaces in full name
if (Space==-1)//if no space was found
{
alert("Not a valid name, we need your full name")
submitOK="False"
}
//checking the characters number 1,2,4,5,7,8,9,10 making sure they are digits.
if ((/[^0-9]/i.(date.charAt(0))) || (/[^0-9]/i.(date.charAt(1))) || (/[^0-9]/i.(date.charAt(3))) || (/[^0-9]/i.(date.charAt(4))) || (/[^0-9]/i.(date.charAt(6))) || (/[^0-9]/i.(date.charAt(7)))
||(/[^0-9]/i.(date.charAt(8))) || /[^0-9]/i.(date.charAt(9)))))
{
alert("Not a valid date of birth")
submitOK="False"
}
if (submitOK=="False")
{
return false
}
}//end of function
//-->
</script>
</head>
<body>
<form name="myForm" onsubmit="return validate()"
onBlur=''
if ((fullname=="")||(date==""))
{
alert(Please do not leave a blank field);
x.fname.focus();
}
>
Enter your full Name: <input type="text" name="fname" size="20"><br />
Enter your date of birth: <input type="text" name="date" size="10"><br />
<input type="submit" value="Submit">
</form>
<script>
<!--
//The resulting output after entering the required correct fields
year=date.substring(5,8);
var s=2005-year;
document.writeln("Hello"+fullname+"Did you know that on your next
birthday you will be"+s+"years old?");
//-->
</script>
</body>
</html>