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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:50:26+00:00 2026-05-27T11:50:26+00:00

I’ve got an input field for a phone number with the option for SMS

  • 0

I’ve got an input field for a phone number with the option for SMS updates. I want to check that when the SMS checkbox is checked that the number is a mobile number. I’ve got the regex working, and this is validating, but have got the ‘mobile require’ error showing even when then check box isn’t checked.

$.validator.addMethod(
      "regex",
      function(value, element, regexp) {
        if($('#receive_sms_updates').is(':checked')) {
          var check = false;
          var re = new RegExp(regexp);
          return this.optional(element) || re.test(value);
        } 
      }, "Mobile Required"
    );


    $("form#patient-detials").validate({
      rules: {
        'patient[person_attributes][phone_mobile]': {
          maxlength: 10,
          minlength: 10,
          digits: true,
          regex: "^04[0-9]{8}" 
        }
      }
    });
  • 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-27T11:50:27+00:00Added an answer on May 27, 2026 at 11:50 am

    Update (from your comment below).

    You aren’t returning true from your custom rule if the if isn’t entered, which technically results in undefined being returned to the caller (which is a falsey value). Update your rule as follows:

    $.validator.addMethod("regex", function(value, element, regexp) {
        if ($('#receive_sms_updates').is(':checked')) {
            var check = false;
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        }
        return true;
    }, "Mobile Required");
    

    Leaving this part around because it might be useful to someone else:

    I would update your validate call and rule as follows:

    $.validator.addMethod("regex", function(value, element, regexp) {
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    }, "Mobile Required");
    
    $("form#patient-detials").validate({
        rules: {
            'patient[person_attributes][phone_mobile]': {
                maxlength: 10,
                minlength: 10,
                digits: true,
                regex: "^04[0-9]{8}",
                required: "#receive_sms_updates:checked"
            }
        }
    });
    

    As you can see, the required property takes a “dependency-expression” which is used to determine whether or not the mobile field is required (based on whether or not #receive_sms_updates is checked).

    Here’s an example: http://jsfiddle.net/andrewwhitaker/ED6cX/


    To take it just one small step further, I would recommend removing the “Mobile Required” method from the rule itself (you want a re-usable regex rule after all), and place it on the messages property on your validate call. Something like:

    $.validator.addMethod("regex", function(value, element, regexp) {
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    });
    
    $("form#patient-detials").validate({
        rules: {
            'patient[person_attributes][phone_mobile]': {
                maxlength: 10,
                minlength: 10,
                digits: true,
                regex: "^04[0-9]{8}",
                required: "#receive_sms_updates:checked"
            }
        },
        messages: {
            'patient[person_attributes][phone_mobile]': {
                required: "Mobile Required",
                regex: "Please enter a valid mobile number"
            }
        }
    });
    

    Example: http://jsfiddle.net/andrewwhitaker/5BVCK/

    This way you aren’t restricting your validation rule (which is generic enough that it could apply to other fields) with a particular form.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I need to clean up various Word 'smart' characters in user input, including but

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.