Page 1 of 4 123 ... LastLast
Results 1 to 10 of 38

Thread: JavaScript Help

  1. #1
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915

    JavaScript Help

    Hey Hey,

    As I've stated before, I'm not a web developer... but I keep getting web dev projects.. So I can navigate PHP pretty good but Javascript is a whole nother world to me.. and I needed input validation... here's the code I'm using

    Code:
    <script  type="text/javascript">
    				function validateProd() {
    										mNv=addImage.sname1.value;
    					if (mNv=='') {
    						alert('Your name is a required field. Please try again.');
    						event.returnValue=false;
    					}
    					mNv=addImage.scode1.value;
    					if (mNv=='') {
    						alert('Your name is a required field. Please try again.');
    						event.returnValue=false;
    					}
    					mNv=addImage.scolour1.value;
    					if (mNv=='') {
    						alert('Your name is a required field. Please try again.');
    						event.returnValue=false;
    					}
    					mNv=addImage.Docket1.value;
    					if (mNv=='') {
    						alert('Your name is a required field. Please try again.');
    						event.returnValue=false;
    					}				
    					mNv=addImage.imRcvd1.value;
    					if (mNv=='') {
    						alert('Your name is a required field. Please try again.');
    						event.returnValue=false;
    					}
    `									}
    				</script>
    and further down I have

    Code:
    <form action="" method="POST" name="addImage" onsubmit="validateProd();">
    Which is how I found it presented on the net... but it's still not working... any ideas/?

    Peace,
    HT

  2. #2
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    Hey Hey,

    So the code has transformed a bit after some searching on google.. but still doesn't work..

    Code:
    				<script language="JavaScript">
    				<!--
    				function validateProd() {
    										if (document.addImage.sname1.value == '') {
    						alert('Your name is a required field. Please try again.');
    						return false;
    					}
    					if (document.addImage.scode1.value == '') {
    						alert('Your name is a required field. Please try again.');
    						return false;
    					}
    					if (document.addImage.scolour1.value == '') {
    						alert('Your name is a required field. Please try again.');
    						return false;
    					}
    					if (document.addImage.Docket1.options[document.addImage.Docket1.selectedIndex].value == '') {
    						alert('Your name is a required field. Please try again.');
    						return false;
    					}				
    					if (document.addImage.imRcvd1.value == '') {
    						alert('Your name is a required field. Please try again.');
    						return false;
    					}
    					return true;
    `									}
    				//-->
    				</script>
    and

    Code:
    <form action="" method="POST" name="addImage" onSubmit="return validateProd();">
    Peace,
    HT

  3. #3
    Senior Member JPnyc's Avatar
    Join Date
    Jan 2005
    Posts
    2,734
    Here's a very generic one I wrote. Should work for any form. If you don't need the passwords matching, you can remove that. The -2 in the test assumes you have 2 buttons, submit and reset.

    function check_form() {

    var frm = document.forms[0];
    var message="";


    var x=0;
    {
    for(var i=0;i< (frm.elements.length-2);i++) {

    if((frm.elements[i].value == "") || (frm.elements[i].value == null)) {

    if(x==5){
    message += "\n";
    }
    message += frm.elements[i].getAttribute('name') + ', ';
    x++;
    }
    }
    if(x!=0){

    alert("The following required fields: "+message+" were left blank");

    frm.elements[i-x].focus();
    return false;
    }
    if(frm.elements[1].value!=frm.elements[2].value) {
    alert('The Passwords Do Not Match');
    return false;
    }
    else {
    return true;
    }
    }
    }

  4. #4
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    Hey Hey,

    Thanks but that still didn't work.. I'm starting to think I've got a problem elsewhere hah... this could take a while.. good thing work is done in 20 minutes and I'm not in tomorrow.

    Peace,
    HT

  5. #5
    Senior Member JPnyc's Avatar
    Join Date
    Jan 2005
    Posts
    2,734
    You must, because I know that script works. Did you remove the passwordst thing? There has to be an error elsewhere in your page. Do the form fields have the name attribute set? Because this script keys off that.

  6. #6
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    Yeah I've got the one that I posted working on other pages I use as well.... Everything is named... but I'm wondering if my naming is causing problems... Unfortunately, due to the nature of the page... the fields are all named (based on their data) and then have a number appended to them with php... I'm doing the same to the javascript and it looks right if I check the source on the page afterwards... but I'm wondering if it's my bastardized melding of php and javascript that's doing it.

  7. #7
    Senior Member JPnyc's Avatar
    Join Date
    Jan 2005
    Posts
    2,734
    A link or the page code might help. But as you said you're leaving soon and you're off tomorrow.

  8. #8
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    both of these worked for me:
    Code:
    <html>
    <head>
    <script language="JavaScript">
    function validateProd() {
       if(document.getElementById('sname1').value == '') {
          alert('Your name is a required field. Please try again.');
          return false;
       }
       if(document.getElementById('scode1').value == '') {
          alert('Your name is a required field. Please try again.');
          return false;
       }
       if(document.getElementById('Docket1').value == '') {
          alert('Your name is a required field. Please try again.');
          return false;
       }
       return true;
    }
    </script>
    </head>
    <body>
    <br />
    <form method="POST" name="addImage" id="addImage" action="index2.php" onSubmit="return validateProd();">
    <input type="text" name="sname1" id="sname1" size="10">
    <input type="text" name="scode1" id="scode1" size="10">
    <select name="Docket1" id="Docket1"><option value=""></option><option value="A">A</option></select>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    Code:
    <html>
    <head>
    <script language="JavaScript">
    function validateProd() {
       if(document.addImage.sname1.value == '') {
          alert('Your name is a required field. Please try again.');
          return false;
       }
       if(document.addImage.scode1.value == '') {
          alert('Your name is a required field. Please try again.');
          return false;
       }
       if(document.addImage.Docket1.value == '') {
          alert('Your name is a required field. Please try again.');
          return false;
       }
       return true;
    }
    </script>
    </head>
    <body>
    <br />
    <form method="POST" name="addImage" id="addImage" action="index2.php" onSubmit="return validateProd();">
    <input type="text" name="sname1" id="sname1" size="10">
    <input type="text" name="scode1" id="scode1" size="10">
    <select name="Docket1" id="Docket1"><option value=""></option><option value="A">A</option></select>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    Does your browser have a javascript console? Perhaps it's throwing some errors that might be helpful. Hope this helps.

  9. #9
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    Hey Hey,

    Still nothing that's working for me... I think i'm javascript inept...

    I opened the JavaScript console.... I've got a few listed errors

    Code:
    Error: validateProd is not defined
    Source File: http://www.xxxx.ca/xxxx/admin/addImages.php
    Line: 1
    Code:
    Error: illegal character
    Source File: http://www.xxxx.ca/xxxx/admin/addImages.php
    Line: 30
    Source Code:
    `									}
    Unfortunately because it's a password protected site for a private client, I can't release a link to the page... here's something interesting though... When I go to view source, the javascript shows up.. but if I double click on the error message and it opens the source and highlights the line, if I look where the javascript should be... the area is blank..

    Peace,
    HT

  10. #10
    Senior Member JPnyc's Avatar
    Join Date
    Jan 2005
    Posts
    2,734
    Try a ridiculously simple JS, like alert('hello woild!'); just to be sure that ANY JS will execute in the environment. IF that doesn't work, we know it's not the coding of the JS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •