JavaScript How do I validate this regex in JavaScript? validate regex,validate regex online,validate regex javascript,validate regex

<input id="phone" name="phone" placeholder="(XXX)XXX-XXXX" type="tel"
       pattern="^(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}$" required="true"/>

How can I validate if that pattern is used in JavaScript for browsers that don't support the pattern attribute?

Thank you for your input -- I've tried doing several options below, but I can't seem to get anything to trace out as "true" -- the RegExp works in the HTML pattern field for FireFox and Chrome. But it's always returning false when I'm trying to utilize it with javaScript?

http://pastebin.com/M0Pdn2Z3

Answer:1

There are a number of polyfills that will enable this in older browsers:

I'd recommend polyfilling -- this doesn't change the behavior for modern browsers but emulates it in old ones. (An aside: the Modernizr polyfills list is fantastic.)

You could also write it yourself; some other answers show how that's done.

Answer:2

Create an onchange event that reads the pattern attribute and runs it against the value.

// Only bind event if we need to
if(!('pattern' in document.createElement('input'))){
    // Bind the event
    document.getElementById('phone').addEventListener('change', function(){
        // Get the regex and value then test it
        var regex = new RegExp(this.pattern),
            val = this.value,
            valid = regex.test(val);

        // Is it valid?
        if(!valid){
            // Do something when it's not
        }
    });
}
Answer:3

You should validate when submitting the form, or losing focus from the input.

If you use jQuery, this will work for all elements with pattern attribute:

$('[pattern]').each(function() {
    if (!$(this).val().match($(this).attr('pattern')))
        alert('Bad value');
});

If not, you can do something for this (similar):

var inputs = getElementsByName('input');
for (var index in inputs) {
    var input = inputs[index];
    var pattern = input.getAttribute('pattern');
    if (pattern != '' && pattern != null) {
        if (!input.value.match(pattern))
            alert('Bad value');
    }
}

These will loop through the attributes that have a pattern to identify against, check them, and alert the user if there's a problem. Of course, you can change the alert to whatever way you would like to handle it (for example, return false to cancel the form submission).

I haven't tested this code, but that's the gist of it.

Answer:4

You can use polyfill or add onchange event to your input.

document.getElementById('phone').addEventListener('change', function(){\
    // returns true if input matches regexp, otherwise it returns false
    var isValid = RegExp(this.pattern).test(this.value);
});
Answer:5

I am currently working on some IE11 compatibility issues. The code is fully functional in compatibility mode as it was previously optimize last for IE5. The affected code is as follows: /* * This ...

I am currently working on some IE11 compatibility issues. The code is fully functional in compatibility mode as it was previously optimize last for IE5. The affected code is as follows: /* * This ...

  1. javascript runtime error '$' is undefined
  2. javascript runtime error function is undefined
  3. javascript runtime error 'jquery' is undefined
  4. javascript runtime error access is denied
  5. javascript runtime error function expected
  6. javascript runtime error 'promise' is undefined
  7. javascript runtime error '__dopostback' is undefined
  8. javascript runtime error invalid character
  9. javascript runtime error 'sys' is undefined
  10. javascript runtime error 'angular' is undefined
  11. javascript runtime error object expected
  12. javascript runtime error out of stack space
  13. javascript runtime error 'json' is undefined
  14. javascript runtime error example
  15. javascript runtime error expected ' '
  16. javascript runtime error 'require' is undefined
  17. javascript runtime error argument not optional
  18. javascript runtime error 'google' is undefined
  19. javascript runtime error hierarchyrequesterror
  20. javascript runtime error invalid template

While implementing angularjs directive i have got some issues in sharing controller between directive i cant access the enterUser directive from the below controller app.directive('entires', [...

While implementing angularjs directive i have got some issues in sharing controller between directive i cant access the enterUser directive from the below controller app.directive('entires', [...

  1. angularjs sharing controller between views
  2. angularjs sharing controller
  3. angularjs sharing same controller

I'm investigating a performance issue we are having with a web form and figured I would do well to solicit some opinions here on this. This page, on load on the server side, retrieves a list of ID ...

I'm investigating a performance issue we are having with a web form and figured I would do well to solicit some opinions here on this. This page, on load on the server side, retrieves a list of ID ...

I tried to get a value from a file with .config extension using the below code, but i am not able to get it........... if i change the file extesion from .config to .xml the its working fine(I am ...

I tried to get a value from a file with .config extension using the below code, but i am not able to get it........... if i change the file extesion from .config to .xml the its working fine(I am ...

  1. read value from dictionary c#
  2. read value from json
  3. read value from appsettings.json
  4. read value from app.config c#
  5. read value from properties file in java
  6. read value from file bash
  7. read value from cell vba
  8. read value from json string in java
  9. read value from dictionary python
  10. read value from web.config c#
  11. read value from file c++
  12. read value from appsettings.json .net core
  13. read value from file python
  14. read value from datatable c#
  15. read value from jobject c#
  16. read value from dataframe
  17. read value from console in java
  18. read value from json file java
  19. read value from application.properties
  20. read value from console in c#