In theoretical computer science and formal language theory, a regular expression (abbreviated regex or regexp) and sometimes called a rational expression is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations. The concept arose in the 1950s, when the American mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep (global regular expression print), a filter.

Wikipedia

In practical terms, i gonna use regex to validate from, before send it to database. This is effective solution, so user that filled form will not wait if form reject submit after submit button pressed.

What kind of criteria i`ve used?

  • username must contain letter AND Number
  • email must valid
  • PIN must only number

Regex: username must contain letter AND Number
^(?=.\d)(?=.[a-zA-Z])
code:

$(document).ready(function() { 
        $('.keyup-numeric').keyup(function() {
            $('span.error-keyup-1').remove();
            var inputVal = $(this).val();
            var numericReg = /^(?=.*\d)(?=.*[a-zA-Z])/;
            if(!numericReg.test(inputVal)) {    
                $(this).after('<span style="color:red;" class=" error-keyup-1"><br>Harus Angka <strong>DAN</strong> Huruf </span>');
        }
        });

Regex: Must valid email
^([\w-.]+@([\w-]+.)+[\w-]{2,4})?$

code:

 $('.keyup-email').keyup(function() {
            $('span.error-keyup-7').remove();
            var inputVal = $(this).val();
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            if(!emailReg.test(inputVal)) {  
                $(this).after('<br><span style="color:red;" class=" error-keyup-7">Invalid Email Format</span>');
            }
        });

Regex: PIn Must Number
^[0-9]+$

code:

$('.keyup-pin').keyup(function() {
            $('span.error-keyup-10').remove();
            var inputVal = $(this).val();
            var ccReg = /\b[0-9](?!.*123456,654321).*0-9\b/;
            if(!ccReg.test(inputVal)) { 
                $(this).after('<span style="color:red;" class="  error-keyup-10">harus 6 ANGKA</span>');
            }
        });


Reference:

designchemical.com
RegExLib.com
xregexp.com

Book:


Regex Tester

regex101.com
regexpal.com
regexr.com
regextester.com

Android Regx Tool
RegEx Generator