Javascript problem

zyreena

New Member
Messages
57
Reaction score
0
Points
0
HTML:
<script language="JavaScript" type="text/javascript">

//function to check empty fields

function isEmpty(strfield1, strfield2, strfield3) {


//change "field1, field2 and field3" to your field names
strfield1 = document.forms[0].field1.value 
strfield2 = document.forms[0].field2.value
strfield3 = document.forms[0].field3.value

  //name field
    if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
    {
    alert("\"Field 1\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //url field 
    if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
    {
    alert("\"Field 2\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //title field 
    if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ')
    {
    alert("\"Field 3\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }
    return true;
}


//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    } 
    return true; 
}


//function that performs all functions, defined in the onsubmit event handler

function check(form)){
if (isEmpty(form.field1)){
  if (isEmpty(form.field2)){
    if (isEmpty(form.field3)){
		if (isValidEmail(form.email)){
		  return true;
		}
	  }
  }
}
return false;
}

</script>

I have downloaded this script from somewhere and tried embedding this in my login.php. IT WORKS. But when i tried saving it as validator.js inside the folder script and called it like:

HTML:
<script type="text/javascript" src='script/validate.js'></script>

and in my form:
HTML:
<form method = 'POST' action = 'session2.php'  onsubmit='return check(this)'>

But it didn't work. Is there something wrong of how i called it? Because there is no error that appear, but the javascript doesn't do anything.
 

kaiweilim

New Member
Messages
26
Reaction score
0
Points
0
Hi there,

erm....
you wrote:
I have downloaded this script from somewhere and tried embedding this in my login.php. IT WORKS. But when i tried saving it as validator.js inside the folder script

However you called it out as script/validate.js

it should be script/validator.js

Regards.
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
Hi there,

erm....
you wrote:
I have downloaded this script from somewhere and tried embedding this in my login.php. IT WORKS. But when i tried saving it as validator.js inside the folder script

However you called it out as script/validate.js

it should be script/validator.js

Regards.

Nice catch, I didn't notice that ;)

You can also do something like this in the head section:
Code:
<script type="text/javascript">
window.onerror=function(msg, url, linenumber) {
    alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    return true;
}
</script>
Which should give you a little bit more helpful information for those tricky JS errors. Just remember to delete it when you're done debugging your script otherwise your users can get annoying error messages.
 
Last edited:

zyreena

New Member
Messages
57
Reaction score
0
Points
0
Hi there,

erm....
you wrote:
I have downloaded this script from somewhere and tried embedding this in my login.php. IT WORKS. But when i tried saving it as validator.js inside the folder script

However you called it out as script/validate.js

it should be script/validator.js

Regards.

I didn't notice that, but still id doesn't work even if i change it to script/validator.js
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
If you have your script in the same directory as your html, then it should be just “validator.js”

HTML:
<script type="text/javascript" src='validator.js'></script>
 

zyreena

New Member
Messages
57
Reaction score
0
Points
0
tnx gomarc but still gomarc no success:

<HTML>
<head>
<script type='text/javascript' src='script/validator.js'></script>

</head>
<body>
<form method = 'POST' action = 'session2.php' onSubmit='return check(this);'>
COLOR: </br>
<input type='text' name='field1'></br>
BODY: </br>
<input type='text' name='field2'></br>
MAKE: </br>
<input type='text' name='field3'></br>
EMAIL: </br>
<input type='text' name='email' id='email'></br>
<input type='submit' value='SUBMIT'>
</form>
</body>
</HTML>
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
Did you try adding my onerror function to your <head>? It should tell you what the problem is.
 
Last edited:

zyreena

New Member
Messages
57
Reaction score
0
Points
0
yah i tried it, but it will return an error on the same line from your code
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
What's the error?

I tested the code just now and I don't get an error.
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
I hope this code works for you as it did for me. I lost track of the small changes made to your code so I’m posting all again. The 3 files are to be placed in the same directory.

HTML:
<html>
<head>
<script type="text/javascript" src="validator.js"></script>
</head>
<form method="post" action="session2.php" onSubmit="return check(this);">
Color:</br>
<input name="field1" type="text" ></br>
Body:</br>
<input name="field2" type="text" ></br>
Make:</br>
<input name="field3" type="text" id="field3" ></br>
E-Mail Address:</br>
<input name="email" type="text" id="email" ></br>
<input name="submit" type="submit" ></br>
</form>
</body>
</html>
validator.js
Code:
//function to check empty fields

function isEmpty(strfield1, strfield2, strfield3) {


//change "field1, field2 and field3" to your field names
strfield1 = document.forms[0].field1.value 
strfield2 = document.forms[0].field2.value
strfield3 = document.forms[0].field3.value

  //name field
    if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
    {
    alert("\"Color\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //url field 
    if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
    {
    alert("\"Body\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //title field 
    if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ')
    {
    alert("\"Make\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }
    return true;
}


//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    } 
    return true; 
}


//function that performs all functions, defined in the onsubmit event handler

function check(form){
if (isEmpty(form.field1)){
  if (isEmpty(form.field2)){
    if (isEmpty(form.field3)){
        if (isValidEmail(form.email)){
          return true;
        }
      }
  }
}
return false;
}
session2.php
PHP:
<?php
echo "Validation was successful.";
?>
 

kaiweilim

New Member
Messages
26
Reaction score
0
Points
0
Wow.. i didn't catch that. Yea... The tags might be the problem with that part. Use <?php and end with ?> instead of <PHP></PHP>

Regards.
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
There shouldn't be any tags :p alert() is a javascript function, not a php function.
 

kaiweilim

New Member
Messages
26
Reaction score
0
Points
0
Really? Guess i wasn't really good in javascript. Still learning in progress. When i see the alert() i thought it is something like echo function of php...:p

Thanks for pointing out my mistake.

Regards.
 
Top