JavaScript - Regex Extracting the Phone number


	 var regex = /(\d{3}[_-]?)(\d{3}[_-]?)(\d{4}[_-]?)/gm
	 var str ="Hey, I am albert. I Have two contact numbers. one is 8989898989 and another one is 989-898-9898"
	 
	 //if you want to exract the mobile number itself from the string
	 str.match(regex);
	 
	 //The output will be
	 ['8989898989', '989-898-9898']
	 

JavaScript - Regex Validating the Phone number


	//Creating the common function
	function validatePhone(str)
	{   
	var regex = /^\(?([0-9]{4})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{3})$/;
	return (str.match(regex))?true:false
	}
	
	var str = "9999999999"
	console.log(validatePhone(str));
	//The output will be
	true
	
	str="(9893)849485"
	console.log(validatePhone(str))
	//The output will be
	true
	
	str="9893-849-485"
	console.log(validatePhone(str));
	//The output will be
	true
	
	str="983598839583945893"
	console.log(validatePhone(str));
	//The output will be
	false
	

JavaScript - Regex validation email


	Creating a common function to validate the email
	function validateEmail(str)
	{
	var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]){2,3}$/g
	return (str.match(regex))?true:false
	}
	
	validateEmail("test@gmail.com")
	//The output will be
	true
	
	validateEmail("test@gmail.com.in")
	//The output will be
	true
	
	validateEmail("test@gmail.com.indd")
	//The output will be
	false
	
	validateEmail("test@gmail.com.in.")
	//The output will be
	false
	

JavaScript - Regex validation string only


	//Creating the common function 
	function validatingStr(str)
	{   
	var reg = /^[A-Za-z]+$/g
	return (str.match(reg))?true:false;
	}
	
	validatingStr("test")
	//The output will be
	true
	
	validatingStr("test1")
	//The output will be
	false
	
	validatingStr("test$$")
	//The output will be
	false