The Frontier Group - Blog

jQuery: Check For Non-Empty Input Fields

November 19th, 2008, by aaron

I was writing some code today and I wanted to prompt the user to check if they wanted to delete a row of data only if there was some data they might not want deleted. Typically to do this I would loop through all the input fields and if any of them weren’t blank I would run the check. I figured that there must be a better way with callbacks or selectors, and after a little thinking I wrote this jQuery snippet :

row.find('input[value!=""]').length > 0

This basically says count the number of inputs with a non-blank value you find, inside the given row.

Just like my last post, it’s nothing amazing but it sure is a better way of doing things. If you look into the andSelf() method then you could also easily chain selects and other input types.

I’m also interested if someone has a better way to approach this problem?

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.

5 Responses to “jQuery: Check For Non-Empty Input Fields”

  1. Tony says:

    Cool idea.

  2. Marko says:

    doesn’t work
    in debugger I can see that length is always 1
    it works if I write code like this:

    row.find(’input[value!=""]‘).val().length > 0

    cheers

  3. Aaron says:

    I’m not sure what to say, my code does work and I’ve tested it in application and also again just now in Firebug. I’d check your HTML for hidden fields, or just do a bit of debugging to find the field that is being reported back to give you a length of 1.

    Your code is actually incorrect as a solution to the problem I was trying to solve here. You’re chaining a single element function against a function that returns an array. In jQuery issuing the val() function against an array of elements will issue the val() function against the first element. In this case it will be an input element that is non-blank, and so your length will always be > 0, as you’re just getting the length of the string.

    In my example I am trying to check if there are any elements that contain some data, so I’m trying to find the length of the array of input elements that aren’t blank.

    Let me know if you have any further questions.

    Cheers :)

  4. anirudha says:

    how this function works

  5. anirudha says:

    please write a blg blog

Leave a Reply


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.