+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Javascript problem

  1. #1
    zyreena is offline x10Hosting Member zyreena is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    57

    Javascript problem

    HTML Code:
    <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 Code:
    <script type="text/javascript" src='script/validate.js'></script>
    and in my form:
    HTML Code:
    <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.
    beginner always starts from the scratch
    www.alliancetutorial.x10hosting.com

  2. #2
    kaiweilim is offline x10Hosting Member kaiweilim is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    26

    Re: Javascript problem

    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.

  3. #3
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Javascript problem

    Quote Originally Posted by kaiweilim View Post
    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 by garrettroyce; 05-24-2009 at 10:17 AM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #4
    zyreena is offline x10Hosting Member zyreena is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    57

    Re: Javascript problem

    Quote Originally Posted by kaiweilim View Post
    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
    beginner always starts from the scratch
    www.alliancetutorial.x10hosting.com

  5. #5
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Javascript problem

    If you have your script in the same directory as your html, then it should be just “validator.js”

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

  6. #6
    zyreena is offline x10Hosting Member zyreena is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    57

    Re: Javascript problem

    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 by zyreena; 05-24-2009 at 10:48 AM.
    beginner always starts from the scratch
    www.alliancetutorial.x10hosting.com

  7. #7
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Javascript problem

    Did you try adding my onerror function to your <head>? It should tell you what the problem is.
    Last edited by garrettroyce; 05-24-2009 at 11:07 AM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  8. #8
    zyreena is offline x10Hosting Member zyreena is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    57

    Re: Javascript problem

    yah i tried it, but it will return an error on the same line from your code
    beginner always starts from the scratch
    www.alliancetutorial.x10hosting.com

  9. #9
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Javascript problem

    What's the error?

    I tested the code just now and I don't get an error.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  10. #10
    zyreena is offline x10Hosting Member zyreena is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    57

    Re: Javascript problem

    ERROR MESSAGE: Object Expected
    URL: http://localhost/phppractice/practice/login.php
    Line Number: 11

    of which line number 11 is:
    <PHP>alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);</PHP>
    beginner always starts from the scratch
    www.alliancetutorial.x10hosting.com

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  3. JavaScript problem
    By anuj_web in forum Programming Help
    Replies: 25
    Last Post: 04-24-2008, 05:15 AM
  4. DB number problem
    By lionheart8 in forum Free Hosting
    Replies: 5
    Last Post: 04-08-2008, 08:26 AM
  5. Problem with ad code (includes?)
    By Salvatos in forum Free Hosting
    Replies: 10
    Last Post: 12-12-2006, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers