Results 1 to 3 of 3

Thread: Advice on Script

  1. #1
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Advice on Script

    New to javascript...I'm looking to see if:

    1. Is there is a better way to do this? Is this a good way or is there something else I should be doing?

    2. I have 8 different fields I have to pair up and check all in sequential order. Example, checking earnings_work1 to earnings_work_occur1, checking earnings_work2 to earnings_work_occur2, etc. Do I need to repeat the script below 8 times or where should I be heading?

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function validateForm()
    {
    
    
    if (form1.earnings_work1.value.length != 0)
    {
    	if (form1.earnings_work_occur1.value.length == 0) {
    		alert("You must enter an occurence.");
    		return (false);
    		}
    
    } 
    }
    //  End -->
    </script>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    function validateForm(){
     for (var z0=1;z0<5;z0++){
      if (form1['earnings_work'+z0].value.length != 0&&form1['earnings_work_occur'+z0].value.length == 0){
       alert("You must enter an occurence.");
       return (false);
      }
     }
    }
    
    /*]]>*/
    </script></head>
    
    <body>
    <form name="form1" >
    <input name="earnings_work1" /><input name="earnings_work_occur1" />
    <br />
    <input name="earnings_work2" /><input name="earnings_work_occur2" />
    <br />
    <input name="earnings_work3" /><input name="earnings_work_occur3" />
    <br />
    <input name="earnings_work4" /><input name="earnings_work_occur4" />
    <br />
    <input type="button" name="" value="Check" onmouseup="validateForm()"/>
    </form>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    tbh I'd just use jQuery with the validation plugin/module.

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
  •