Julian Herbold
Julian Herbold

Reputation: 537

Javascript method to check for digits within a string (validation of input data)

Im trying to implement a validation for an input field in IBM BPM. Im not really familiar with java script but I try to get method that returns ture if a string contains any numbers.

awdadw = valid
awdawd2d = invalid

I tried this method:

function hasNumbers(t)
{
    var pattern=new RegExp("^[A-Za-z]+$"); 
    return pattern.test(t); // true if string. Returns false for numbers
}

When I try this function, it says that method / variable RegExp is unknown. Since it is rather basci stuff I hope to get some sources where this topic is explained.

Upvotes: 0

Views: 1463

Answers (2)

Julian Herbold
Julian Herbold

Reputation: 537

Based on adre3wap this worked for me:

function validate(t){
  var re = /^[A-Za-z]+$/;
  if(re.test(t))
     return false;
  else
     return true;
}

Upvotes: 0

yardie
yardie

Reputation: 1557

You can use this:

function validate(){    
var re = /^[A-Za-z]+$/;
if(re.test(document.getElementById("inputID").value))
   alert('Valid Name.');
else
   alert('Invalid Name.');      
  }

Upvotes: 2

Related Questions