user2514925
user2514925

Reputation: 949

Regular Expression to Allow Only Numbers

i want to give validation condition like,

         var BlkAccountIdV = $('#txtBlkAccountId').val();
     if (BlkAccountIdV == "") {            
        document.getElementById('lblAccountId').innerText = "Enter Valid AccountID";
        errorflag=1;
    } 

if condition should be Executed only when entered text box value contains Letter.What value can i give inside Quotation marks(if (BlkAccountIdV == "") )?

Upvotes: 1

Views: 18244

Answers (3)

Alex
Alex

Reputation: 1304

var re = /[^0-9]/g;
var error = re.test(BlkAccountIdV);

error will be true if value of BlkAccountIdV is not numeric

I.e, this regex will match everything except numbers

So your code should look somehow like this:

var BlkAccountIdV = $('#txtBlkAccountId').val();
var re = /[^0-9]/g;    
if ( re.test(BlkAccountIdV) ){  
    // found a non-numeric value, handling error
    document.getElementById('lblAccountId').innerText = "Enter Valid AccountID";
    errorflag=1;
}

Upvotes: 2

mujaffars
mujaffars

Reputation: 1479

In if condition you can use isNaN() function. This checks whether string is 'not a number'

So in your case if this condition not valid, Then string is number

if(!isNaN(BlkAccountIdV) && BlkAccountIdV != ''){
  //Your code
}

Upvotes: 0

MTJ
MTJ

Reputation: 1097

if (BlkAccountIdV.match(/[0-9]+/) == null )

Upvotes: 0

Related Questions