JavaScript Multiline Validation with Regular expression

Posted by Unknown On Tuesday, January 6, 2015 0 comments

How to validate multiple line values using JavaScript and Regular Expression.
Problem: I want to validate if my textarea contains only number. But my textarea accepts multiple Number values separated by break line.
1234
6743
2934
2134

Solutions:

  1. Merge the value into single line of code by replacing the end of line character with empty string
  2. Check if the value is number using regular expression

JavaScript
function isNumber(value){
   var pattern = new RegExp("^[0-9]+$");
   value = value.replace(/\n/g,"");
   return pattern.test(value);
}

0 comments:

Post a Comment