View Single Post
Old 11-18-2004, 04:23 PM   #1 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Easy JavaScript question

My question is how can I make sure the user enters a name of 5-15 chars, a password with at least 1 uppercase and one lowercase, and display the date in mm/dd/yyyy form? I have figured out some stuff .... here is my code


Code:
<html>
<head>
<title>Test 3 - JavaScript</title>


<script type="text/javascript" language="JavaScript">
<!-- 
	function validate()
		{
			if (!document.form.name.value || !document.form.password.value ||!document.form.date.value || !document.form.phone.value || !doucment.form.name.value<=5)
				{
					alert("All fields are not entered");
					document.form.name.focus();
					return false;
				}
			else
				{
					return true;
				}
		}		
	
	function name()
	{
		if(document.form.name.length < "5" || document.form.name.length > "15")
		{
			alert("Your password is either to long or to short");
			document.form.name.focus();
			return false;
		}
		
		else
			{
				return true;
			}
	}
	
	function formatNumber(){
    var theNumbersOnly = "";
    var theChar = "";
    var theInput = document.form.phone.value;
    
    for (i = 0; i < theInput.length; i++){
        theChar = theInput.substring(i,i+1);
        if (theChar >= "0" && theChar <= "9"){
            theNumbersOnly = "" + theNumbersOnly + theChar;
        }
    }
    
    if (theNumbersOnly.length < 10){
        alert("You must enter 10 numbers.");
        document.form.phone.focus();
    }else{
        var areacode = theNumbersOnly.substring(0,3);
        var exchange = theNumbersOnly.substring(3,6);
        var extension = theNumbersOnly.substring(6,10);
        var newNumber = "(" + areacode + ") ";
        newNumber += exchange + "-" + extension;

        document.form.phone.value = newNumber;
    }
}
//-->
</script>


</head>
<body>
* All fields MUST be completed.
<form name="form" method="post" action="q1.php" method="POST" onsubmit="return validate();" onsubmit="return validate2();">
  <p><strong>Name: </strong> 
    <input name="name" type="text" id="name" maxlength="15" minlength="5" onchange="name();">
  (5-15 characters)<br>
    <br>
  <strong>Password: 
  <input name="password" type="password" id="password" maxlength="15" minlenth="5">
  <br>
  <br>
  Phone: 
  <input type="text" name="phone" value="(555) 555-1234" size="20" onchange="formatNumber();">
  </strong>(10 digits)<strong><br>
  <br>
  Date: <input name="date" type="text" name="textfield"> <br>
  <br>
  <input type="submit" name="Submit" value="Submit">
  </strong></p>
  </form>
<br>
</body>
</html>
keystoneman is offline   Reply With Quote