-1

I want to give validation in my form fields..

Fields:

  1. Name (textbox) --> Should enter the value. When without enter & come out it will show error message.

  2. Email(regex) -->checking the mail format

  3. Gender(radio button) -->any one radio button should selected

  4. Location(dropdown) --> without enter dropdown will show error message

  5. Mobile Number(textbox) --> enter only numbers

The above all types of validation I need.

$('#Email').click(function(){
            var name=$('#Name').val();
            if(name==""){
                $('#Name').after('<div class="red">Name is Required</div>');

            }
            });

But whenever i click email box it shows error message.How to do it? Please Help me? I tried 1 st field Name

PoliDev
  • 1,408
  • 9
  • 24
  • 45
  • 1
    hey you are doing the click fucntino for email, but has the check for #Name, Is this you required. if so, This may be happening 1. Name field is empty, 2. Now when you click on #Email field, then #Name field is validating. Why can't you use jquery.validator.js plugin, it will suit your needs. – RONE Jul 19 '13 at 06:50
  • 2
    Search for blur(),selectedIndexChanded() events and email regex `/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/` and search how to use it. – imVJ Jul 19 '13 at 06:51

3 Answers3

2

You aren't supposed to do it on the click event of the email textbox, but on the blur event of the name textbox. The blur event is fired whenever a control loses focus. Try this,

$("#Name").blur(function(){
    if($(this).val().length === 0){
        $(this).after('<div class="red">Name is Required</div>');
    }
});

Here I am checking the length of the value inside the #Name textbox, whenever it loses focus. Now when you are submitting your form, you'll have to make this check again when the user presses the button click event.

Similarly to validate the email field, you'll use the regex that @onlyVJ gave.

/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/

You'll get the value from the textbox and match it with the regex, using match. Follow this link to learn more. This will be done on the blur event of the #Email textbox

EDITED -

I won't be giving you code but, I'll be instructing you on an approach that you can use.

For dropdown -

You can attach a blur event to dropdown, and check the whether the default value that you show in the beginning when the page loads is still the selected value. If so, show the error, else don't.

For radio buttons -

Check this jsFiddle. Grabbed from here. You should be able to use that to configure your code to display error when radio button is not checked.

Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64
1

You need to use the blur event to identify that an element has lost focus. Visit the documentation for blur for more information and an example.

If you want you could also try the jQuery validation plugin. Visit the jQuery Validation Plugin website

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
0

something like this...

$("#element").blur(function(){
   $(this).validate({
      // your code here
   })
})
Ganesh Rengarajan
  • 2,006
  • 13
  • 26