Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JavaScript Function

Posted on 2009-05-14
1
512 Views
Last Modified: 2013-11-19
I am receving errors on the checks for Age and Weight in my userinfo.js file
I have put both my userinfo.html and userinfo.js in the code below.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">
 
<!-- userinfo.html    a document for userinfo.js
      -->
 
<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title> Illustrates form Input Validation </title>
      <script type = "text/javascript" src = "userinfo.js" >
      </script>
   </head>
   <body>
   <h3> Customer Information </h3>
   <form action = "">
 
   	<p>
   		<label>
   			<input type = "text" id = "custName" />
   			Name (last name, first name, middle initial)
   		</label>
   		<br /><br />
 
 
   		<label>
   			<input type = "text" id = "phone" />
   			Phone number (ddd-ddd-dddd)
   		</label>
   		<br /><br />
 
 
   		<label>
   			<input type = "text" id = "custAge" />
   			Age (Must be 18 or older)
   			</label>
   			<br /><br />
 
 
   		<label>
   			<input type = "text" id = "custWeight" />
   			Weight (Must be between 80-300)
   			</label>
   			<br /><br />
 
 
 
 
   		<input type = "reset" id = "reset" />
 
   		<input type = "submit" id = "submit" />
   	</p>
   </form>
 
   <!-- An inline script for the event handler registrations -->
   		<script type = "text/javascript">
   		   <!--
   // Set form element object properties to their
   // corresponding event handler functions
 
 
   		document.getElementById("custName").onchange = chkName;
   		document.getElementById("phone").onchange = chkPhone;
   		document.getElementById("custAge").onchange = chkAge;
   		document.getElementById("custWeight").onchange = chkWeight;
   // -->
 
    </script>
 
   </body>
</html>
 
 
 
// userinfo.js
// an example of input validation using the change and submit events
 
// The event handler function for the name text box
 
function chkName() {
	var myName = document.getElementById("custName");
	
	// Test the format of the input name
	// Allow the spaces after the commas to be optional
	// Allow the period after the inital to be optional
		var pos = myName.value.search(
			/^[A-Z][a-z]+, ?[A-Z][a-z]+, ?[A-Z]\.?$/);
		if (pos != 0) {
			alert("The name you entered (" + myName.value +
				") is not in the correct form. \n" +
				"The correct form is: " +
				"last-name, first-name, middle-intial \n" +
				"Please go back and fix your name");
		myName.focus();
		myName.select();
		return false;
		} else
		  return true;
	}
	
	// The event handler function for the phone number text box
	
function chkPhone() {
	var myPhone = document.getElementById("phone");
		
	  // Test the format of the input phone number
		var pos = myPhone.value.search(/^\d{3}-\d{3}-\d{4}$/);
		if (pos != 0) {
			alert("The phone number you entered (" + myPhone.value +
			") is not in the correct form. \n" +
			"The correct format is ddd-ddd-dddd \n" +
			"Please go back and fix your phone number");
		myPhone.focus();
		myPhone.select();
		return false;
		} else
		  return true;
		}
		
		
// The event handler function for checking the age
	
function chkAge() {
	var myAge = document.getElementById("custAge");
	
	//Test the format
	var pos = myAge.value.search();
	if (pos < 18)	{
		alert("The age you entered (" + myAge.value + 
		") is not in the correct form. \n" +
		"The correct format is 18 years and older \n" +
		"Please go back and fix your age");
		myAge.focus();
		myAge.select()
		return false;
		
	} else
	return true;
}
 
 
// Checking the Weight for correct format
 
function chkWeight() {
	var myWeight = document.getElementById("custWeight");
	
	// test format
	var pos = myWeight.value.search();
	if (80 > pos > 300)	{
		alert("The weight you entered (" + myWeight.value +
		") is not in the correct form. \n" +
		"The correct format is between 80-300. \n" +
		"Please go back and fix your weight");
		myWeight.focus();
		myWeight.select()
		return false;
		} else
		return true;
	}

Open in new window

0
Comment
Question by:ChadAnglin
1 Comment
 
LVL 9

Accepted Solution

by:
ftaco96 earned 250 total points
ID: 24385991
1) You don't need the .search() to get the values from the textboxes
2) You should use an or ("||") to test weight values ......  if (80 > pos || pos > 300) {


function chkAge() {
      var myAge = document.getElementById("custAge");

      //Test the format
      var pos = myAge.value;
      if (pos < 18)      {



// Checking the Weight for correct format

function chkWeight() {
      var myWeight = document.getElementById("custWeight");

      // test format
      var pos = myWeight.value;
      if (80 > pos || pos > 300)      {
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change div area and length 1 34
Porbably easy CSS question about overlapping and offset images 21 36
Word, Excel, Access icons 4 17
Html input text color the frame line 2 20
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question