# How to create test to check for valid characters in response

**URL:** <https://community.postman.com/t/how-to-create-test-to-check-for-valid-characters-in-response/5324>\
**Category:** Help Hub\
**Created:** [May 7, 2019, 2:53pm UTC](https://community.postman.com/t/how-to-create-test-to-check-for-valid-characters-in-response/5324 "2019-05-07T14:53:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lhuntsinger](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lhuntsinger](https://community.postman.com/u/lhuntsinger)\
**Post date:** [May 7, 2019, 2:53pm UTC](https://community.postman.com/t/how-to-create-test-to-check-for-valid-characters-in-response/5324/1 "2019-05-07T14:53:41Z")

</div>

I have been trying to check certain fields in my response to see if they contain valid characters. Below is an example:

`"Manufacturer": "MAJOR PHARMACEU"`

I want to make sure that ONLY these characters are present (0-9, a-z, A-Z, “-”, “\_”) if any other characters are present, I want to pass a FAILED message. I tried using the code below in the test section, but it is not returning the correct results:

```
var manufacturer = response.LineItems[0].Manufacturer;
if (/^[0-9a-zA-Z\-_]+$/.test(manufacturer) === true){
    pm.test("Manufacturer field contains invalid characters");
}
else {
    pm.test("Manufacturer field contains valid characters");
}

```

Any directions would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![bhargavkaranam96](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@bhargavkaranam96](https://community.postman.com/u/bhargavkaranam96)\
**Post date:** [May 8, 2019, 7:42am UTC](https://community.postman.com/t/how-to-create-test-to-check-for-valid-characters-in-response/5324/2 "2019-05-08T07:42:39Z")

</div>

Hi @lhuntsinger,  
Assuming the response is JSON, you can make modifications to your code like this

```javascript
let response = pm.response.json(),
  manufacturer = _.get(response, 'LineItems.0.Manufacturer'); // similar to response.LineItems[0].Manufacturer

// In case manufacturer does not contain valid characters, this particular test will fail and you can view the test results in Tests tab
pm.test("Manufacturer field contains invalid characters", () => {
   pm.expect(/^[0-9a-zA-Z\-_]+$/.test(manufacturer)).to.be.true;
});

```

You can go through the following links for examples and documentation on how to write test scripts in Postman. Also about the inbuilt libraries (Ex: Lodash)

[https://learning.getpostman.com/docs/postman/scripts/test\_scripts](https://learning.getpostman.com/docs/postman/scripts/test_scripts)  
[https://learning.getpostman.com/docs/postman/scripts/test\_examples](https://learning.getpostman.com/docs/postman/scripts/test_examples)  
[https://learning.getpostman.com/docs/postman/scripts/postman\_sandbox/#commonly-used-libraries-and-utilities](https://learning.getpostman.com/docs/postman/scripts/postman_sandbox/#commonly-used-libraries-and-utilities)
