-3

I want to validate the content entered inside the textarea (SimpleMDE) editor. Code should display error if users enter any Indian mobile number inside the text editor.

function validate() {
  var regMobile = new RegExp('^[6-9]\d{9}$');
  var description = document.getElementById('ph').value;

  if (regMobile.test(description)) {
    document.write('phone number exists ' + description);
  } else {
    document.write('phone number not exists ' + description);
  }
}
<form name="eve">
  <textarea id="ph"></textarea>
  <button onclick="validate()">Click me</button>
</form>

e.g. input:

9882223456,
8976785768,
7986576783

Sample snippet. Reference Regex from the Url

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
appsntech
  • 722
  • 1
  • 6
  • 21
  • So what exactly is the question? – Pointy Jan 28 '19 at 16:00
  • 1
    To be clear, you're asking if your current regex will match all phone numbers from india? Spoiler alert: it won't – Kai Qing Jan 28 '19 at 16:00
  • ok, how do i validate the content to stop users to enter mobile number ? @KaiQing – appsntech Jan 28 '19 at 16:02
  • You don't. You just moderate the content after it is entered and create your filters on a server side. Think of how many ways someone can enter a phone number - (0) 11 - 0 39 - One Eight Hundred, 011039, etc. your regex would be flawed no matter what you try – Kai Qing Jan 28 '19 at 16:03
  • Looks like you'd need to escape the backslash from `\d` for your regex to work. Tested in my chrome dev console : `new RegExp('^[6-9]\d{9}$').test("9882223456")` evaluates to false, while `new RegExp('^[6-9]\\d{9}$').test("9882223456")` evaluates to true. Or you can use the `/pattern/` syntax : `/^[6-9]\d{9}$/.test("9123456789")` works too – Aaron Jan 28 '19 at 16:03
  • @Aaron Reference please ? – appsntech Jan 28 '19 at 16:06
  • @Aaron User can enter mix of content. They may even enter some content and followed by their phone number. I want system to validate in frontend before we save. – appsntech Jan 28 '19 at 16:09
  • @appsntech it's just standard string evaluation : the backslash is understood as the escape character, and an escaped `d` is a `d`. The RegExp constructor receives the `^[6-9]d{9}$` string which doesn't match what you want. Try matching `9ddddddddd` if you want, you'll see it matches – Aaron Jan 28 '19 at 16:10
  • @appsntech concerning your last comment : not a problem at all, you must have misunderstood me. I'm just saying if you want to use the `RegExp(string)` constuctor you have to escape your backslashes, i.e. use `\\d` instead of `\d`. That or use the `/pattern/` syntax instead of the `RegExp(string)` constructor (but then it's the forward slashes you have to escape...) – Aaron Jan 28 '19 at 16:12

2 Answers2

1

You need to check if the field contains a valid number. This is a relatively simple task. You will need to use String.prototype.match to see if you can find the number. If the matcher comes back with a result, you will know you have a valid number.

Note: I borrowed Angular_PG's regular expression.

const PATTERN_MOBILE_INDIA = /\b([0|+[0-9]{1,5})?([7-9][0-9]{9})\b/;

function validate() {
  var desc = document.getElementById('ph').value;
  var match = desc.match(PATTERN_MOBILE_INDIA);
  var contains = match != null;
  var exists = contains ? ('exists: ' + match[0]) : 'does not exist.';
  var el = document.querySelector('.validation-text');
  el.innerHTML = 'Valid phone number ' + exists;
  el.classList.toggle('input-valid', contains);
  el.classList.toggle('input-invalid', !contains);
}
span { display: block; }
.input-valid { color: #080 }
.input-invalid { color: #A00  }
<form name="eve" onSubmit="return false;">
  <textarea id="ph" rows="3" cols="40">Hello 9882223456, how are you?</textarea>
  <button onClick="validate()">Validate</button>
  <span class="validation-text"></span>
</form>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Try below regex. This would help to validate Indian mobile number.

 function validateMobile(mobilenumber) {   
    var regmm='^([0|+[0-9]{1,5})?([7-9][0-9]{9})$';
    var regmob = new RegExp(regmm);
    if(regmob.test(mobilenumber)){
        return true;
    } else {
        return false;
    }    
}
PraveenG
  • 35
  • 1
  • 7
  • Why wouldn't you just use a native expression? `/^([0|+[0-9]{1,5})?([7-9][0-9]{9})$/` and just return the test... `return regmob.test(mobilenumber)` – Mr. Polywhirl Jan 28 '19 at 16:09