Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
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
Old 11-19-2004, 07:29 AM   #2 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
You'd do yourself some good learning about regular expressions.

Also, it's a bad idea to name form elements things that have meaning to the DOM. Such as calling one "name" when all elements have a name attribute. It confuses some browsers when you try to reference the form element. Plus, you have a function called "name". Confusing people who need to maintain code after you is not very nice. Plus, this might additionally confuse a browser. When you reference "name", do you mean the attribute, the form field, or the function? At best, it's not a good programming practice. At worst, it will break something.

DOCTYPE and CSS are your friends.

You should put your HTML through a validator. AFAIK, there is no attribute "minlength" for a text field.
http://www.w3.org/TR/html401/interact/forms.html#h-17.4

onsubmit and method had double listings. You can't do that.

You test phone number for 10 chars, yet the initial value has more than that. Is the user supposed to enter 10 numbers or are they supposed to enter a formatted string (your initial value is already formatted and thus more than 10 characters)?

Hiding script is no longer needed. The last browser to not understand script elements was like Netscape 3.

I'm not sure what you mean about displaying the date - users have to enter it in a particular format, for the most part. You have to know how they entered it to know how to transform it. How would you know that they didn't enter it in dd/mm/yyyy format, as is so common in the UK?
I'll leave the date stuff up to you.
As to the rest...Here's how I would do this.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test 3 - JavaScript</title>

<script type="text/javascript">
function isBlank(field)
   {
   // True if val is whitespace only or empty.
   var re = /\S+/;
   return (re.test(field));
   }

function checkName(field)
   {
   /* 5 to 15 alphanumeric characters, inclusive */
   var re = /^\w{5,15}$/;
   if (!re.test(field))
      {
  		alert("The name field must be between 5 and 15 alphanumeric characters. ");
		field.focus();
		return false;
      }
   else return true;
   }

function checkPsswd(field)
   {
   /* 5 to 15 characters, inclusive */
   var re_num = /^\w{5,15}$/;
   var re_case = /^\w*([a-z]+\w*[A-Z]+|[A-Z]+\w[a-z]+)\w*$/;
   if (!re_num.test(field))
      {
  		alert("The password field must be between 5 and 15 alphanumeric characters.");
		field.focus();
		return false;
      }
   if (!re_case.test(field))
      {
  		alert("The password field must have at least one lower case letter and at least one upper-case letter.");
		field.focus();
		return false;
      }

   else return true;
   }

function validate(frm)
   {
   if (isBlank(frm.uname.value) ||
       isBlank(frm.psswd.value) ||
       isBlank(frm.date.value)  ||
       isBlank(frm.phone.value))
      {
   	alert("All fields are not entered");
   	frm.focus();
   	return false;
   	}
   
   formatNumber(frm.phone.value);
   return (checkName(frm.uname.value) &&
           checkPsswd(frm.psswd.value))
   }	
		
function formatNumber(field)
   {
   /* if blank, don't do anything */
   if (isBlank(field)) return;

   /* user must enter 10 digits or a fully formatted number. If already formatted, exit. */
   var re_digits = /^\d{10}$/;
   var re_formatted = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/;
   if (re_formatted.test(field)) return;

   /* got here, need to format */
   // check is 10 digits
   if (!re_digits.test(field))
      {
      alert("Phone number must be 10 digits or in the form (123) 555-5555");
      return;
      }
   // okay, got 10 digits - format them to (123) 555-5555
   var newNumber = "(" + field.substring(0,3) + ") " + field.substring(3,6) + "-" + field.substring(6,10);
   field.value = newNumber;
   }
</script>
</head>
<body>
* All fields MUST be completed.
<form name="frm1" method="get" action="" onsubmit="return validate(this);">
<p><strong>Name: </strong> 
<input name="uname" type="text" id="uname" maxlength="15" onchange="checkName(this.value);">
(5-15 characters)<br>
<br>
<strong>Password: 
<input name="psswd" type="password" id="psswd" maxlength="15" onchange="checkPsswd(this.value);">
<br>
<br>
Phone: 
<input type="text" name="phone" value="(555) 555-1234" size="20" onchange="formatNumber(this.value);">
</strong>(10 digits)<strong><br>
<br>
Date: <input name="date" id="date" type="text"> <br>
<br>
<input type="submit" name="Submit" value="Submit">
</strong></p>
</form>
<br>
</body>
</html>
kaeli69 is offline   Reply With Quote
Old 11-19-2004, 08:09 AM   #3 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Oops - code correction

I should really fully test these things before I post them.

Correction to code:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test 3 - JavaScript</title>

<script type="text/javascript">
function isBlank(field)
   {
   // True if val is whitespace only or empty.
   var re = /\S+/;
   return (field.length==0 || !re.test(field));
   }

function checkName(field)
   {
   /* 5 to 15 alphanumeric characters, inclusive */
   var re = /^\w{5,15}$/;
   if (!re.test(field))
      {
  		alert("The name field must be between 5 and 15 alphanumeric characters. ");
		field.focus();
		return false;
      }
   else return true;
   }

function checkPsswd(field)
   {
   /* 5 to 15 characters, inclusive */
   var re_num = /^\w{5,15}$/;
   var re_case = /^\w*([a-z]+\w*[A-Z]+|[A-Z]+\w[a-z]+)\w*$/;
   if (!re_num.test(field))
      {
  		alert("The password field must be between 5 and 15 alphanumeric characters.");
		field.focus();
		return false;
      }
   if (!re_case.test(field))
      {
  		alert("The password field must have at least one lower case letter and at least one uppwe-case letter.");
		field.focus();
		return false;
      }

   else return true;
   }

function validate(frm)
   {
   if (isBlank(frm.uname.value))
      {
   	alert("Name required");
   	frm.uname.focus();
   	return false;
   	}
   if (isBlank(frm.psswd.value))
      {
   	alert("Password required");
   	frm.psswd.focus();
   	return false;
   	}
   if (isBlank(frm.phone.value))
      {
   	alert("Phone required");
   	frm.phone.focus();
   	return false;
   	}
   if (isBlank(frm.date.value))
      {
   	alert("Date required");
   	frm.date.focus();
   	return false;
   	}

   formatNumber(frm.phone.value);
   return (checkName(frm.uname.value) &&
           checkPsswd(frm.psswd.value))
   }	
		
function formatNumber(field)
   {
   /* if blank, don't do anything */
   if (isBlank(field.value)) return;

   /* user must enter 10 digits or a fully formatted number. If already formatted, exit. */
   var re_digits = /^\d{10}$/;
   var re_formatted = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/;
   if (re_formatted.test(field.value)) return;

   /* got here, need to format */
   // check is 10 digits
   if (!re_digits.test(field.value))
      {
      alert("Phone number must be 10 digits or in the form (123) 555-5555");
      return;
      }
   // okay, got 10 digits - format them to (123) 555-5555
   var newNumber = "(" + field.value.substring(0,3) + ") " + field.value.substring(3,6) + "-" + field.value.substring(6,10);
   alert(newNumber);
   field.value = newNumber;
   }
</script>
</head>
<body>
* All fields MUST be completed.
<form name="frm1" method="get" action="" onsubmit="return validate(this);">
<p><strong>Name: </strong> 
<input name="uname" type="text" id="uname" maxlength="15" onchange="checkName(this.value);">
(5-15 characters)<br>
<br>
<strong>Password: 
<input name="psswd" type="password" id="psswd" maxlength="15" onchange="checkPsswd(this.value);">
<br>
<br>
Phone: 
<input type="text" name="phone" size="20" onchange="formatNumber(this);">
</strong>(10 digits)<strong><br>
<br>
Date: <input name="date" id="date" type="text"> <br>
<br>
<input type="submit" name="Submit" value="Submit">
</strong></p>
</form>
<br>
</body>
</html>
kaeli69 is offline   Reply With Quote
Old 11-22-2004, 11:20 AM   #4 (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
Thank you kaeli69 ... I am working with your code to edit things and I am still tryin to learn more JavaScript do you know of any free tutorials that I could use as a starting point?
keystoneman is offline   Reply With Quote
Old 11-23-2004, 06:29 AM   #5 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Quote:
I am working with your code to edit things and I am still tryin to learn more JavaScript do you know of any free tutorials that I could use as a starting point?
There are a ton of tutorials (Google for them), most of which don't tell you the most important things - how to deal with the fact that different browsers support different DOM models.

To really do a good job, take a look at the official docs for the two most popular browsers and note some of the main differences.

Microsoft IE:
http://msdn.microsoft.com/library/de...ence_entry.asp

Gecko (Netscape, Mozilla, Firefox):
http://www.mozilla.org/docs/dom/domr..._shortTOC.html

The most popular way to do things these days is with document.getElementById. Yet older browsers, such as Netscape 4, do not support this. So don't be surprised if you simply can't do some things in older browsers. To make sure your stuff doesn't crash old browsers (it won't work, but it won't crash them either) is to use object detection. NOT BROWSER DETECTION. Browser detection (using the user agent string) is bad for a crapload of reasons I won't get into here (usenet is your friend if you care to look up the numerous threads on the topic).
If you want to use any of the DOM methods, do this:
Code:
if (document.getElementById)
   {
   // whatever you need to do with it
   }
And usenet is your friend. Take a look at the comp.lang.javascript FAQ. Loads of good stuff there.
http://jibbering.com/faq/

HTH
kaeli69 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript alert without refreshing? sde HTML, XML, Javascript, AJAX 2 06-30-2004 12:52 PM
javascript question sde HTML, XML, Javascript, AJAX 10 08-11-2003 03:36 AM
Javascript Function jcramer83 HTML, XML, Javascript, AJAX 5 04-14-2003 07:12 AM
Easy Question Ilya020 Standard C, C++ 1 03-02-2003 06:31 PM


All times are GMT -8. The time now is 03:20 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting