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 8950677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:29:41+00:00 2026-06-15T13:29:41+00:00

I am setting up my error message on my form with jquery and when

  • 0

I am setting up my error message on my form with jquery and when I go and view it it does not ask that the name field be required but just the email even though I have included it. Here is my code:

<form method="post" action="" id="contactform">
            <fieldset class="first">


            <div id="response"></div>  


            <div class="name_input">                
            <input name="name" id="name" class="required" type="text" onfocus="if(this.value == 'Name'){this.value = '';}" onblur="if(this.value == ''){this.value='Name';}" value="Name" maxlength="128" />                
            </div>



            <div id="email_input">              
            <input id="email" name="email" onfocus="if(this.value == 'Email'){this.value = '';}"  onblur="if(this.value == ''){this.value='Email';}" class="required" type="text" value="Email" maxlength="128" />         
            </div>

            <div id="button">
            <input type="submit" class="button" name="Submit" value="" />
            </div>


            </fieldset>


        </form>

and my JS:

$(document).ready(function() {

$('form #response').hide();

$('#button').click(function(e) {


    e.preventDefault();


    var valid = '';
    var required = ' is required.';
    var name = $('form #name').val();
    var email = $('form #email').val();



    if (name == '' || name.length < 2) {
        valid = '<p>Your name' + required +'</p>';  
    }

    if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
        valid += '<p>Your email' + required +'</p>';                                                  
    }



    if (valid != '') {

        $('form #response').removeClass().addClass('error')
            .html('<strong>Please correct the errors below.</strong>' +valid).fadeIn('fast');           

    }


    else {

        $('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');                                      

        var formData = $('form').serialize();
        submitForm(formData);           
    }           

  });
});

There are not any conflicts with my name tag anywhere else so I dont know what may be happening

  • 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-06-15T13:29:41+00:00Added an answer on June 15, 2026 at 1:29 pm

    This line is the culprit

    if (name == '' || name.length < 2) {
    

    When the field has 1 character, it will evaluate to false, and not bother to check the next condition for name.length

    You can simplify the code by just having this

    if (name.length < 2) {
    

    This will also work if name=''.

    Also, to trim away leading and trailing spaces, you might want to get the name field like this:

    var name = $.trim( $('form #name').val() );
    

    That way just two or more spaces in the field will also trigger the validation.

    Update:

    The trouble is you have a value in the input field already. You are using a focus/blur event to remove/add it as needed. That will count as a value with length = 4 when you are validating it.

    There are a couple of ways to solve it.

    1. Use placeholder. It is supported in all modern browsers like this:

      <input type="text" id="name" placeholder="Name">

    2. Add a further condition to the validation for name like this:

      if (name === 'Name' || name.length < 2 ) {

    This should hopefully, fix your problems

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

Sidebar

Related Questions

I have a form element that I'm setting as required: $this->addElement('text', 'email', array( 'label'
So I'm setting up my form validation, inputs, error messages etc... and then realized
This is probably a setting error somewhere. I have a django app that works
Lint Error Checking tool that comes with Android Developer Tools has an optional setting
I'm using jQuery's AJAX function to send a message from a contact form -
WHat's the best way/code in Login Form (Zend_Form) to display error message 'Login/password incorrect'?
My problem is, that the browsers' (IE&FF) autocomplete does not work for my login
I am having trouble setting my ContentProperty to Text. The error I am given
What is the proper way to do error-checking in a class? Raising exceptions? Setting
When using scipy.optimize 's fmin I'm getting an error I don't understand: ValueError: setting

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.