Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / September 2006



Tip: Looking for answers? Try searching our database.

Form element validating

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DoomedLung - 28 Sep 2006 12:25 GMT
Hey,

I'm currently developing an online work brief, where the user has to
enter details regarding the project into a form. I've come to a section
in the form where the user has the option of selecting  between two
checkboxes and an input element. I have to ensure that at least one of
the checkboxes have been checked or that the input element is not
empty. I've come up with this function:

function validateMedia(){
    var count = 0;
    if(document.workBrief.mediaOnline.checked){
        count++;
    }
    if(document.workBrief.mediaPrint.checked){
        count++;
    }
    if(document.workBrief.mediaOther.value != ""){
        count++;
    }
    if(count > 0){
        return true;
    }else{
        alert('Please choose at least one media option');
        return false;
    }
}

I'm sure there is a more effiecient way of writing this.

You thoughts are much appriciated :)
Randy Webb - 28 Sep 2006 16:00 GMT
DoomedLung said the following on 9/28/2006 7:25 AM:
> Hey,

<snip>

> I'm sure there is a more effiecient way of writing this.

There is.

> You thoughts are much appriciated :)

My thoughts on your code or my thoughts on you posting the same basic
question 3 times with 3 different subject lines? The latter wouldn't be
fit to be read in a family oriented environment.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

DoomedLung - 30 Sep 2006 13:35 GMT
> DoomedLung said the following on 9/28/2006 7:25 AM:
> > Hey,
[quoted text clipped - 16 lines]
> comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
> Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

I apologise for submitting the question 3 times, Google seemed to not
be up updating the topic. So I assumed it didn't work, Alas I jumped
the gun and ended up looking like a twat.
RobG - 01 Oct 2006 00:52 GMT
[...]
> I apologise for submitting the question 3 times, Google seemed to not
> be up updating the topic. So I assumed it didn't work, Alas I jumped
> the gun and ended up looking like a twat.

Yeah, there was a period when you first posted that Google Groups was
taking about 3 hours to update, I'm surprised there weren't more
duplicate posts.

Signature

Rob

Hoss - 28 Sep 2006 18:47 GMT
function validateMedia()
{
  // get handles to your 3 elements
  if(el.checked || el2.checked || el3.value.length > 0)
  {
      // we are good
  }
  else
  {
    // no input
  }
}
Dr John Stockton - 28 Sep 2006 19:29 GMT
JRS: In article <1159442701.861642.216770@e3g2000cwe.googlegroups.com>,
dated Thu, 28 Sep 2006 04:25:02 remote, seen in
news:comp.lang.javascript, DoomedLung <doomedlung@googlemail.com> posted

>function validateMedia(){
>       var count = 0;
[quoted text clipped - 14 lines]
>       }
>}

Test this :

function validateMedia() { var X = document.workBrief,    
 OK = X.mediaOnline.checked ||
      X.mediaPrint.checked ||
      X.mediaOther.value != ""
 if (!OK) alert('Please choose at least one media option');
 return OK }

See  <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

It's a good idea to read the newsgroup and its FAQ. See below.
Signature

© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.