Ionic2: Extending string functions


#1

Hi,

I would like to extend the string functions. Therefore i created the following structure:
-src
-app
- extensions
-string.ts

My string.ts files contains the following

String.prototype.isValidEmail = function() {
    var re= /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(this);
};

interface String {
    isValidEmail(): boolean;
}

If i now use this function i get this error : this.email.isValidEmail is not a function, most likely because does not include it in the build process. My question is where should i include this, so this function would be recognized?


#2

I would like to know to implement this in a Angular/Ionic way too. Did you find a solution?


#3

See this SO topic.


#4

Any solution??

I’am dealing with the same problem :frowning:

Thanks!!


#5

In Angular I include these kinds of things in my polyfill.ts file, but in Ionic this doesn’t seem to be available for editing (polyfills.js is created in the build process from somewhere inaccessible). For now I’m including them in main.ts, but it doesn’t really feel quite right.