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
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!
Thank you @kevinc-postman . I used the second approach .It worked fine.