tags:

views:

47

answers:

5

Sounds simple but it's giving me grief:

Tried this:

function validateAddress() {

    if (!($('<%=txtPlaceName.ClientID%>').val() === "")
         || !($('<%=txtStreet.ClientID%>').val() === "")
         || !($('<%=txtAddress.ClientID%>').val() === "")
         || !($('<%=txtPostcode.ClientID%>').val() === "")) {

        return true;
    }

    return false;
}

and this:

function validateAddress() {

    if ($('<%=txtPlaceName.ClientID%>').val().length > 0
         || $('<%=txtStreet.ClientID%>').val().length > 0
         || $('<%=txtAddress.ClientID%>').val().length > 0
         || $('<%=txtPostcode.ClientID%>').val().length > 0) {

        return true;
    }

    return false;
}

but neither seem to work, am I doing it correctly?

A: 

It looks like you're missing the "#" in your jquery selectors for ids. Try this:

if ($('#<%=txtPlaceName.ClientID%>').val().length > 0 
     || $('#<%=txtStreet.ClientID%>').val().length > 0 
     || $('#<%=txtAddress.ClientID%>').val().length > 0 
     || $('#<%=txtPostcode.ClientID%>').val().length > 0) { 
John Fisher
+2  A: 

You're forgetting the hash mark to select something by id. Try:

function validateAddress() {

    if ($('#<%=txtPlaceName.ClientID%>').val().length > 0
         || $('#<%=txtStreet.ClientID%>').val().length > 0
         || $('#<%=txtAddress.ClientID%>').val().length > 0
         || $('#<%=txtPostcode.ClientID%>').val().length > 0) {

        return true;
    }

    return false;
}
Ken Browning
case closed, I seem to forget that a lot! Thanks!
SLC
@SLC - While this does solve the issue, you can make your life much easier, check out my alternative using a class. You can also move all your code outside the page with this method.
Nick Craver
@Nick - While your idea appears tidier, class should really be used for CSS, when pages are written and passed to the design team they might mess it up!
SLC
A: 

are you adding # to the ID of the element when you check it, in other words, is the produced text like this:

$('#IDOfElement').val()
Anatoly G
+2  A: 

For a #ID selector you need a #, like this:

$('#<%=txtPlaceName.ClientID%>').val().length

But the quicker way could be to give them a class, e.g. CssClass="checkMe", then check those elements:

function validateAddress() {
  return $('.checkMe[value!=""]').length > 0;
}
Nick Craver
Thank you for cleaning that up. Every time a programmer regurgitates something senselessly, god kills a kitten.
mway
@mway: I hate cats. ;-)
Chris
Rather than using a CSS class, you could also use the HTML5 Web Forms 2.0 attribute "required", which will be more semantically correct and future-compatible
cbeer
A: 

I've been using classes to group functionality, so for your example give each of the element a class of address then your function would be:

function validateAddress() {
  $('.address').each(function() {
    if ($(this).val().length > 0) {
      return true;
    }
  }
  return false;
}
jsuggs