SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script that requires 2 matching fields to have a value???

    Hi All

    My JavaScript knowledge isn't too bad but it isn't that good. I can normally pick code apart and make minor alterations but I am not at the point to write something from scratch.

    I have an image upload form that allows 5 images at a time to be uploaded. Each image can have a caption. I need to make the caption field required.

    As the form names and values are set as an array they are all the same so I think I need to a script to be generic to say something like 'Please add a caption to every image'.

    The script I need has to say basically if image_filename is not empty then image_caption must not be empty.

    If anyone can point me in the direction of a script that would do this or could give me an example it would be greatly appreciated.

    Many Thanks

    M Barnes

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No takers for this as yet, seems that others are as confused as I am

    try posting your existing code and it may be easier to understand the requirement

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Nebraska
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    If I understand your question right, you simply want a form checker that will not let a user submit the form if they enter an Image without entering a caption. If that conclusion is true, you could do something like this:

    Code:
    <html>
    <head>
    <title>Picture Upload</title>
    <script language="javascript1.2">
    
    function checkForm(){
         
    	 if(document.pictureForm.imageName1.value!=""&&document.pictureForm.caption1.value==""){
    	      alert("You must enter a caption for every picture.");
    		  return false;
    	 }
    	 else{
    	      if(document.pictureForm.imageName2.value!=""&&document.pictureForm.caption2.value==""){
    	           alert("You must enter a caption for every picture.");
    		       return false;
    	      }
    	      else{
    	           if(document.pictureForm.imageName3.value!=""&&document.pictureForm.caption3.value==""){
    	                alert("You must enter a caption for every picture.");
    		            return false;
    	           }
    			   else{
    	                if(document.pictureForm.imageName4.value!=""&&document.pictureForm.caption4.value==""){
    	                     alert("You must enter a caption for every picture.");
    		                 return false;
    	                }
    					else{
    	                     if(document.pictureForm.imageName5.value!=""&&document.pictureForm.caption5.value==""){
    	                          alert("You must enter a caption for every picture.");
    		                      return false;
    	                     }
    						 else
    	                          return true;
    	                }
    	           }
    	      }
    	 }
    }
    
    </script>
    </head>
    
    <body>
    <form name="pictureForm" onSubmit="return checkForm();" method="post" action="">
      Image
      <input name="imageName1" type="text" id="imageName1">
      Caption     
      <input name="caption1" type="text" id="caption1"><br>
      Image
      <input name="imageName2" type="text" id="imageName2">
      Caption     
      <input name="caption2" type="text" id="caption2"><br>
      Image
      <input name="imageName3" type="text" id="imageName3">
      Caption     
      <input name="caption3" type="text" id="caption3"><br>
      Image
      <input name="imageName4" type="text" id="imageName4">
      Caption     
      <input name="caption4" type="text" id="caption4"><br>
      Image
      <input name="imageName5" type="text" id="imageName5">
      Caption     
      <input name="caption5" type="text" id="caption5"><br>
      <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    It is kind of long, and I realize that you probably could make a loop that would do the samething in a lot less code, but I prefer to stick to simple, easy to understand code, especially when giving it to people who are trying to learn JavaScript as I still am.

    Louie

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With a couple of minor adjustments to fit with my form it works perfectly.

    Thank you Louie so very much for your help.

    M Barnes


Bookmarks

Posting Permissions

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