Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:09:40+00:00 2026-05-28T11:09:40+00:00

I have this code: <form action= id=credentials method=post> <label for=name id=nameLabel >Name*:</label> <input type=text

  • 0

I have this code:

<form action="" id="credentials" method="post">
<label for="name" id="nameLabel" >Name*:</label>
<input type="text" name="name" id="name" value=""  />
<select id="single">
   <option value="0">Single</option>
   <option value="1">Single2</option>
</select>
<input type="submit" value="Submit" name="" data-inline="true"/>
</form>

And I’m trying to validate like this:

$("#credentials").submit(function(e) {
    var nameField = $("input#name").filter(function() {
        return !$.trim(this.value).length;
    });

    if (nameField.length) {
        nameField.css("border", "1px solid red");
        alert("Name is missing!");

        return false;
    }

    var stateField = $("#single").val();

    if (stateField == 0) {
        stateField.css("border", "1px solid red");
        alert("State is missing!");
        return false;
    }
});

The input is working fine, but not the select. Any ideas? Here is a JSFiddle example

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:09:41+00:00Added an answer on May 28, 2026 at 11:09 am

    The problem is that your stateField variable holds the actual value of the field. It is not a reference to the field itself or a jQuery object for the field so you can’t say stateField.css().

    Try something like this:

    var stateField = $("#single");
    
    if (stateField.val() == 0) {
        stateField.css("border", "1px solid red");
        alert("State is missing!");
        return false;
    }
    

    Updated version of your demo: http://jsfiddle.net/A6eEh/3/

    Note also that by returning from your function as soon as you find an error your validation will only ever report one error at a time (so the stateField validation won’t occur unless you’ve entered a name).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code: <form name=form action=./ method=post> <input type=file name=newfile id=newfile />
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
in the form action i have the following line\ <form action={URL} method=post target=_self id=xxx
I have this code: <p class = foo>Text</p> And I also have a form:
so i have this code: <% form_tag(:action => 'find') do%> Product name: <%= text_field(cars_,
I have this html form <form action=upload/ id=upload name=upload> // other form data </form>
I have this code in my JSP: <%@taglib uri=http://struts.apache.org/tags-html prefix=html%> .. .. <html:form action=update
I have this code <html> <head> <script type=text/javascript src=my-search.js></script> </head> <body onLoad=my_Init();> <div> <form
I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray
I have this form submit code: Event.observe(window, 'load', init, false); function init() { Event.observe('addressForm',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.