0

I want to check my URL that has exactly a specific format. I have this URL

www.example.com?rt=12365

I want to check if income URL has below format

?rt= number

then do something. I use the router like my below code:

const queryParams = router.url

but I don't know how to check it. I'm new to typescript and angular, please help me

1

2 Answers 2

3

Try this

 constructor(private route: ActivatedRoute) {

 }

 if (this.route.snapshot.queryParams['rt']) {
      if ( !isNaN(this.route.snapshot.queryParams['rt']) && angular.isNumber(this.route.snapshot.queryParams['rt'])) 
       {
         }
 }

Hope useful for you

Sign up to request clarification or add additional context in comments.

6 Comments

How do I check that there is a number in front of the rt and nothing more؟
@heliyarb also might be worth checking out Lodash, quite a nice library with a lot of useful functions: lodash.com/docs/4.17.15#isNumber
what is angular in this code? I get Cannot find name 'angular' error
import {isNumeric} from "rxjs/util/isNumeric" var isNumber = isNumeric(this.route.snapshot.queryParams['rt']) cehck this
add it but still has an error. I think it's for angular.js. I use angular 8.
|
2

You can use queryParams

constructor(private route: ActivatedRoute) {

}
ngOnInit() {
    this.route.queryParams.subscribe(params => {
        console.log(params.rt);
        if(!isNaN(params.rt)){
            console.log("URL Match");
        }else{
            console.log("URL does not Match");
        }
    });
}

4 Comments

this code just gets rl to me. I need to check that if URL exactly has "rt=number" format
you can check if(params.rt) for value of rt is exist or no
should I check like this: has("rt")?
so how to check that front of rt is number?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.