Jump to content

Help javascript newbie


cs1h

Recommended Posts

Hi,

 

I am very new to javascript and I tried following a tutorial on how to make sure all the fields in a form are filled in, however I cannot get it to work for my form.

 

My script is,

 

<script language="JavaScript" src="formValidator.js">
</script>
<script language="JavaScript">
// check form values
function checkForm()
{
// instantiate object
fv = new formValidator();
// perform checks
// check for empty name field
// check for empty email address
if (fv.isEmpty(document.myform[0].elements[0].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for valid email address format
if (!fv.isEmpty(document.myform[0].elements[0].value) &&
!fv.isEmailAddress(document.myform[0].elements[0].value))
{
fv.raiseError("Please enter a valid email address");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[1].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[2].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[3].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[4].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[5].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[6].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for empty name field
if (fv.isEmpty(document.myform[0].elements[7].value))
{
fv.raiseError("You must fill out all of the fields, except Photo (#, which is optional");
}
// check for checkbox
if (!fv.isChecked(document.myform[0].elements[9]))
{
fv.raiseError("You must agree to the Terms of Use and Privacy Policy");
}
// all done
// if errors, display, else proceed
if (fv.numErrors() > 0)
{
fv.displayErrors();
return false;
}
else
{
return true;
}
}
</script> 

 

The mentioned formValidator.js file is as follows,

 

<script language="JavaScript">

// add an error to error list
function raiseError(msg)
{
this.errorList[this.errorList.length] = msg;
}

// return number of errors in error array
function numErrors()
{
return this.errorList.length;
}

// display all errors
// iterate through error array and print each item
function displayErrors()
{
for (x=0; x= min && val <= max)
{
return true;
}
else
{
return false;
} 
}
// check to see if input is a valid email address
function isEmailAddress(val)
{
if (val.match(/^([a-zA-Z0-9])+
([.a-zA-Z0-9_-])*@
([a-zA-Z0-9_-])+
(.[a-zA-Z0-9_-]+)+/))
{
return true;
}
else
{
return false;
} 
}
// check to see if form value is checked
function isChecked(obj)
{
if (obj.checked)
{
return true;
}
else
{
return false;
} 
}
// display all errors
// iterate through error array and print each item
function displayErrors()
{
for (x=0; x<this.errorList.length; x++)
{
alert("Error: " + this.errorList[x]);
}
}
// add an error to error list
function raiseError(msg)
{
this.errorList[this.errorList.length] = msg;
}
// return number of errors in error array
function numErrors()
{
return this.errorList.length;
}
// end object

</script> 

 

Does anyone know why this isn't working, all help is much appriciated.

 

thanks

Colin

 

Link to comment
Share on other sites

There are a few easy-to-fix problems with this. First of all, you shouldn't have HTML tags in your external javascript file (delete '<script language="JavaScript">' and '</script>' in formValidator.js'). The real problem, though, is that your formValidator functions are not actually part of an object. You have to define the formValidator object as an encompassing object in formValidator.js, then define all those other functions under it. It'll look something like this:

 

function formValidator() {
//Define any default object variables out here. Examples:
//this.valid = false;
//this.errCount = 0;

// add an error to error list
this.raiseError = function(msg)
{
  this.errorList[this.errorList.length] = msg;
}

// return number of errors in error array
this.numErrors = function()
{
  return this.errorList.length;
}

//All the rest of the functions...
}

 

Those two things should fix it. If not, you'll probably have to define your problem a little more specifically.

 

-kael

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.