1

I want to check my field is nil, then return true otherwise check email validation. My field name is app_email.

$.validator.addMethod("checkEmail", function(app_email, element) {
        var result;
        if (condition) {
            result = (app_email === "NIL") ? true : false;  
        } else {
            preg_match( /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/,app_email);
            return true;
        }
    }, 
    "Email id is not valid."
);

html

                      <div class="control-group">
                       <label class="control-label">E-mail ID<span class="required">*</span></label>
                            <div class="controls">
                                  <input type="text" class="span6 m-wrap"  name="app_email" title="Enter Email Id">  
                            </div>
                       </div>
4
  • you want to your field to allow user if he didnt enter anything.. if he enters shuld check for validation right ? Commented Sep 28, 2015 at 6:24
  • You can use radio button check inside custom validation function. See my answer here with working demo Commented Sep 28, 2015 at 6:31
  • cant you just use input type email, it will take care of rest Commented Sep 28, 2015 at 6:32
  • You can do this without writing a single line of js code by using <input type="email" /> :) Commented Sep 28, 2015 at 6:32

2 Answers 2

2

If you are using jQuery Validation, you can use the default function like this.

You can refer to this link.

$( "#myform" ).validate({
  rules: {
    fieldNameHere: {
      required: true,
      email: true
    }
  }
});

Check if the value is 'NIL' return true, otherwise, validate email.

$.validator.addMethod("checkEmail", 
    function(value, element) {
        return value === "NIL" ? true : /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(value);
    }, 
    "Email id is not valid."
    );
Sign up to request clarification or add additional context in comments.

4 Comments

I want to check my field is nil
What do you want to check, field value or field name?
I want to check my field value is nil,that is allowed
Check my edit, you can use checkEmail instead of email in rules.,.
2

Demo

You can use radio button check inside custom validation like below.

JS:

jQuery.validator.addMethod("checkEmail", function (value, element) {
        if ($('input:radio').is(':checked')) {
            if (/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test(value)) {
                return true;
            } else {    
                return false;
            }
        } else {
            return true;
        }
        
    }, "Email id is not valid.");

    $('#myform').validate({
        rules: {
            email: {
                checkEmail: true
            }
        }
    });

HTML:

<form id="myform">
    <input type="radio" value="">
    <input name="email" id="email" type="text"/>
    <input type="submit" />
</form>

In this example, email validation will be perform if the radio button is selected.

2 Comments

What is the type then? I have used one radio button and an email text input. If you provide the field type, I can update demo and answer
That is for email field, what about check input? Could you please put your html also in your question?

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.