0

I have a JavaScript String, for example :

var test1 = "[email protected]" ;  // Function must return true in this case

var test2 = "[email protected] " ;    // Function must return false in this case

I would like to build a function which return true if the String contains @ character, following by any character "bbbbbb" following by ".temp" extension.

How can I do this ?

2 Answers 2

5

You can use regex#test

var found = /@.*?\.temp\b/.test(str);

If you want to make it @ character, following by "bbbbbb" following by ".temp" extension then use:

var found =  /@bbbbbb\.temp\b/.test(str);
Sign up to request clarification or add additional context in comments.

6 Comments

email addresses are case insensitive
I believe @.*\.temp$ would be more efficient (.* vs .*?).
@sp00m: Probably that is but both of them should work functionally.
@anubhava Not exactly, yours will match also [email protected].
@sp00m: Oh yes I was more talking from .* vs .*? angle. For that I have added \b after \.temp
|
0

Regular expression solution:

function validate(argument) { 
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,}))$/;
    if((argument.lastIndexOf('.temp', argument.length-5)>=argument.length-5) && re.test(argument)) {
        return true;
    } else {
        return false;
    }
}

JsFiddle test: http://jsfiddle.net/2EXxx/

5 Comments

@M42 just take a look at my code. =\ and don't minus if you even didn't read a code...
@SimonWhitehead just take a look.
@M42 and what about this version?
@M42 Fixed a bit. Accepts only VALID characters for any types of email. BTW in your solution you may replace 'bbbb' with any characters according to the authors question. Anyway thank you for attendion but minus makes me sad (
OK, I removed my -1. But there're no needs to validate email address in the question. It's just wanted to test if it ends with .temp.

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.