Verify a variable value is numeric , the variable is string

I have an Variable in the response of type String , but the value of the String should always be numeric

usercode :β€œ23456789”;
I need to verify if is always numeric , if any alpha numeric value , then i should fail the case
tried
pm.expect((usercode)).to.be.a(β€œnumber”);
but its not working properly

Hi @anita.christobel !

I have two ideas that could work for you :pray:

Using a Regular Expression : You can use a regex pattern to check if the string only contains numbers. If the string matches this pattern, it means it’s numeric.

// Adjust this based on where you get 'usercode' from
var usercode = pm.response.json().usercode; 

pm.expect(usercode).to.match(/^\d+$/);

The ^\d+$ regex pattern will only match strings that have one or more digits and nothing else.

Another Approach with isNaN() : The JavaScript function isNaN() can be used to check if something is Not-a-Number. You can use it in conjunction with the Number() function to achieve your validation:

// Adjust this based on where you get 'usercode' from
var usercode = pm.response.json().usercode; 

pm.expect(isNaN(Number(usercode))).to.be.false;

Here, we’re first converting usercode to a number and then using isNaN() to check if it’s not a number. The expectation is that it should always be a number, hence the .to.be.false assertion.

Hope this helps! :smile:

Thank you @kevinc-postman . I used the second approach .It worked fine.