1

sorry for opening what might be a basic question for a more seasoned jquery user but I am learning - I am currently on form validation and want to move on to a little bit more advanced methods, Im working with a tutorial and I like the tutorial it is just that they lack a bit of explanation hench me turning here:

Ok we have a basic form

enter image description here

Form submitted with errors

enter image description here

Code

<form method="post" name="productform" id="productform" >
    <p>
        <label for='prodid'>Product ID: (maxlength 10)</label><br>
        <input type="text" name="prodid" >
    </p>
    <p>
        <label for='email'>Email: (minlength 10)</label><br>
        <input type="text" name="email" >
    </p>
    <p>
        <label for='address'>Address( length between 10 and 250)</label><br>
        <input type="text" name="address">
    </p>    
    <p>
        <label for='message'>Message:( length between 50 and 1050)</label> <br>
        <textarea name="message"></textarea>
    </p>

    <input type="submit" name='submit' value="Post it !">
</form>

JQUERY

$(function()
{
    $("#productform").validate(
      {
        rules: 
        {
          prodid: 
          {
            required: true,
            maxlength: 10
          },
          email: 
          {
            required: true,
            email: true,
            minlength:10
          },
          address:
          {
            required: true,
            rangelength:[10,250]
          },
          message: 
          {
            rangelength:[50,1050]
          }
        }
      });   
});

CSS

body
{
  font-family: Arial, Sans-serif;
}
.error
{
color:red;
font-family:verdana, Helvetica;
}

My Question

When the form is submitted with errors the red error message is displayed next to the textbox? I fail to see in the Jquery code where the error message is created....I would understand if you have a span element or similar next to the textbox and then append the error to that element I fail to see where it happens in code above...

Im sure Im missing something simple or basic here...but I cant move on until I grasped this so it is rather frustrating.

1

2 Answers 2

2

The jQuery validation plugin is used here, so error messages are automatically handled by this plugin.

It means that you don't need to use a span or another element to wrap error messages, this is handled by the plugin.

And in the given jquery function, the validtaion rules are specified so the form will be validateddepending on these rules.

And this CSS is used to style those messages:

.error {
    color:red;
    font-family:verdana, Helvetica;
}
Sign up to request clarification or add additional context in comments.

Comments

2

As you can see in the code, they have used Jquery validate plugin.

Since rule is given as required: true for these three fields this error is coming.

$("#productform").validate(
      {
        rules: 
        {
          prodid: 
          {
            required: true,
            maxlength: 10
          },
          email: 
          {
            required: true,
            email: true,
            minlength:10
          },
          address:
          {
            required: true,
            rangelength:[10,250]
          },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.