-2

How can I check if an input field value is in a specific shape like This : String@string.string

without using the input type email.

  • 1
    1. You can use regex 2. You probably shouldn't be validating emails. Just leave it to ` – VLAZ May 11 '21 at 17:07
  • I can't use . it is not allowed in the exam : – Mohamed_Iyed May 11 '21 at 17:09
  • Does this answer your question? [How to validate an email address in JavaScript](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – evolutionxbox May 11 '21 at 17:09
  • I am not in the exam right now , but i want to know an esay technic to check the validation of an email using javascript , i cant use this regular expression : /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; – Mohamed_Iyed May 11 '21 at 17:16

1 Answers1

0

using patterns:

<input type="text" pattern="some_regex"/>

in your case:

<input type="text" pattern="[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+"/>

or just

<input type="text" pattern="\S+@\S+\.\S+"/>

if you want to use javascript, you can use:

function validateInput(){
  const regex = /some_regex/
  return regex.test(inputElement.value)
}
// example
function validateInput(){
  const regex = /\S+@\S+\.\S+/
  return regex.test(inputElement.value)
}
Allure
  • 513
  • 2
  • 12
  • 1
    Thank you so much ... sorry about the non clear question – Mohamed_Iyed May 11 '21 at 17:25
  • Please , can you tell me is the function .match() specifise a shape or a forma .... like if i add this to the .match() parameter : .match(/[A-z]+@[A-z]+.[A-z]{1,}/) the input must be like this : string(non limited length)@string(non limited length).string(length at least 1) ?? – Mohamed_Iyed May 11 '21 at 17:46
  • @MRNOMERCY string.match returns first occurence of substring that passes regex test, if you use //g ( global flag ) it returns every occurence, so output is an array, for example 'hello'.match(/l/) returns ["l", index: 2, input: "hello", groups: undefined], but 'hello'.match(/l/g) returns ["l", "l"], so it searches not tests, more about match metod you can read here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match – Allure May 11 '21 at 18:20