Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy forms and input fields

    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 ?

    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>

  • #2
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    please ...!! any answers, I am desperate......

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I'm not entirely sure what you're trying to achieve with this.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>51276</title>
    
    		<script type="text/javascript">
    			function validate()
    			{
    				var submitOK = true;
    				var name = document.getElementById("name");
    
    				if(name.value.length < 3 || name.value.search(/\s/gi) < 0 || name.value.search(/[0-9]/gi) > -1)
    				{
    						submitOK = false;
    						alert("Not a valid name, we need your full name");
    				}
    
    				var year = document.getElementById("year").value;
    				var month = document.getElementById("month").value;
    				var date = document.getElementById("date").value;
    
    				if(date.length < 1 || isNaN(date) || year.length < 4 || isNaN(year))
    				{
    					submitOK = false;
    				}
    
    				if(submitOK)
    				{
    					var date = new Date();
    					var age = date.getFullYear() - year;
    
    					document.getElementById("age").replaceChild(document.createTextNode(age), document.getElementById("age").firstChild);
    					document.getElementById("age").parentNode.style.display = '';
    				}
    
    				//return submitOK;
    				return false;
    			}
    		</script>
    	</head>
    	<body>
    		<form id="form" name="myForm" onsubmit="return validate()">
    			<fieldset>
    				<div>
    					<label for="name">Name:</label>
    					<input id="name" name="name" type="text">
    				</div>
    
    				<fieldset>
    					<legend>Date of birth</legend>
    
    					<label for="date">Date</label>
    					<input id="date" name="date" type="text" size="2" maxlength="2">
    
    					<label for="month">Month</label>
    					<select id="month" name="month">
    						<option value="0">Jan</option>
    						<option value="1">Feb</option>
    						<option value="2">Mar</option>
    						<option value="3">Apr</option>
    						<option value="4">May</option>
    						<option value="5">Jun</option>
    						<option value="6">Jul</option>
    						<option value="7">Aug</option>
    						<option value="8">Sep</option>
    						<option value="9">Oct</option>
    						<option value="10">Nov</option>
    						<option value="11">Dec</option>
    					</select>
    
    					<label for="year">Year</label>
    					<input id="year" name="year" type="text" size="4" maxlength="4">
    				</fieldset>
    
    				<input type="submit" value="Submit">
    			</fieldset>
    		</form>
    
    		<p>Did you know that on your next birthday you will be <span id="age"> </span> years old?</p>
    
    		<script type="text/javascript">
    			document.getElementById("age").parentNode.style.display = 'none';
    		</script>
    	</body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •