-2

I've to check if my string value is ending with ".com" or ".de" I've put this values inside array:

var valuesEnd = [".com", ".de"]

My value to compare is taken from form. Also it has to check for @ sign, it must have.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
George
  • 70
  • 8

3 Answers3

1

Use RegExp#test.

var str = ['abc.de', 'wok.pl', 'qwdok.com'];

console.log(str.map(v => /\w+\.(de|com)$/g.test(v) ? v + ' is valid' : v + ' is invalid'));
kind user
  • 40,029
  • 7
  • 67
  • 77
0

You can use a regular expression like this:

var str = prompt("Email: ");

if(/\.(?:com|de)$/.test(str))
  alert("'" + str + "' is valid");
else
  alert("'" + str + "' is not valid");
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
0

I've created a jQuery plugin for you:

(function($) {
  // the string object (NOTE: it can be extended)
  $.string = {};
  // "create" a new string
  $.string.new = function(string) {
    return string;
  };
  // test a string for a regex match
  $.string.test = function(string, regex) {
    return regex.test(string);
  };
  // join three strings together
  $.string.join = function(a, b, c) {
    return a + b + c;
  };
  // log a string to the console
  $.string.log = function(string) {
    console.log(string);
  };
})(jQuery);

Then you would use the plugin like this:

// for test purposes we'll be using: "example@example.com"
var email = $.string.new("example@example.com");
// check to see if the email is valid or not
if($.string.test(email, /\@.*?\.(?:com|de)$/)) {
  // let us know that the email is valid
  $.string.log($.string.join("'", email, "' is a valid email."));
}
// if this block runs the email is invalid
else {
  // let us know that this is an invalid email
  $.string.log($.string.join("'", email, "' is not a valid email."));
}