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 :: How To Open Phone Dialer and Make Call From React Native App 
Javascript :: upload image react 
Javascript :: get element type javascript 
Javascript :: adding styling to element using javascript 
Javascript :: js array add every element of array 
Javascript :: js double exclamation mark 
Javascript :: how to check if exists in javascript 
Javascript :: js click counter 
Javascript :: how to install vue 
Javascript :: image preview before upload jquery 
Javascript :: jquery chek radio 
Javascript :: javascript check if two arrays contain same values 
Javascript :: setting timeout in javascript 
Javascript :: what is currying in javascript 
Javascript :: react append classname 
Javascript :: build apk from react native 
Javascript :: moment get iso week number 
Javascript :: tribonacci sequence javascript 
Javascript :: get file extension nodejs 
Javascript :: install vue by CDN 
Javascript :: double click in js 
Javascript :: vue get if checkbox is checked 
Javascript :: how to set the development mode in webpack 
Javascript :: convert a new date standard to a yyy-mm-dd format in javascript 
Javascript :: how to compare objets in an array 
Javascript :: anagram javascript example 
Javascript :: how to filter json array in javascript 
Javascript :: how to put react compnent to bottom 
Javascript :: js does object contain value 
Javascript :: get element by xpath 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =