Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to validate a string using regular expression in javascript

function validate(){
  var phoneNumber = document.getElementById('phone-number').value;
  var postalCode = document.getElementById('postal-code').value;
  var phoneRGEX = /^[(]{0,1}[0-9]{3}[)]{0,1}[-s.]{0,1}[0-9]{3}[-s.]{0,1}[0-9]{4}$/;
  var postalRGEX = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
  var phoneResult = phoneRGEX.test(phoneNumber);
  var postalResult = postalRGEX.test(postalCode);
  alert("phone:"+phoneResult + ", postal code: "+postalResult);
}

Comment

javascript validate string with regex

console.log(/^([a-z0-9]{5,})$/.test('abc1')); // false

console.log(/^([a-z0-9]{5,})$/.test('abc12')); // true

console.log(/^([a-z0-9]{5,})$/.test('abc123')); // true
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: axios get error response message 
Javascript :: javascript format rupiah 
Javascript :: sequelize delete item 
Javascript :: remove first row from table jquery 
Javascript :: add item to list javascript 
Javascript :: nodejs mysql connection pool 
Javascript :: compose es6 
Javascript :: add char in specific index stirng javascript 
Javascript :: remove node module 
Javascript :: csrf token in js laravel 
Javascript :: default input type date limit date js 
Javascript :: how to submit form using ajax 
Javascript :: how to make hide/show btn in js 
Javascript :: month list javascript 
Javascript :: convert array to object javascript 
Javascript :: javascript append item to array 
Javascript :: multiline comment in react 
Javascript :: react save to local storage 
Javascript :: mongoose query if field exists where filed exists 
Javascript :: count number of divs inside a div javascript 
Javascript :: js convert truthy 
Javascript :: MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 
Javascript :: ckeditor get value 
Javascript :: javascript get nth element of array 
Javascript :: Remove duplicate items form array using reduce() method. 
Javascript :: react history go back 
Javascript :: getting data from form node 
Javascript :: javascript take first element of array 
Javascript :: get url in js 
Javascript :: how to get selected value of dynamically created dropdown in jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =