Question: I am trying to accept an email without having whitespace or blank at the end.
I have tried two variants

> 
> pattern="^[A-Za-z0-9.\_%+-]+@[a-z0-9.-]+.[a-z]{1,4}[^\s]+$">
> 
> 
> pattern="^[A-Za-z0-9.\_%+-]+@[a-z0-9.-]+.[a-z]{1,4}\S$">
> 
> 
> 

both are not working & input is accepting whitespace at the end. My full input line is 

> 
> input type="email" id="guestUserEmail" name="guestEmail" data-pattern-error="Email is invalid" data-required-error="Please enter email address" required pattern="^[A-Za-z0-9.\_%+-]+@[a-z0-9.-]+.[a-z]{1,4}$"
> 
> 
> 

I referred this answer [regex for no whitespace at the begining and at the end but allow in the middle](https://stackoverflow.com/questions/34974942/regex-for-no-whitespace-at-the-begining-and-at-the-end-but-allow-in-the-middle)

Please suggest!

Answer: 
Update
======

HTML by default collapses whitespace. This means:

* If there are more than one whitespace between chars, it will render as a single whitespace char.

	+ ex.`this string has a double space right here .`   
	will render as:  

	`this string has a double space right here .`
* Leading and trailing whitespace are stripped (this doesn't happen with strings hence the necessity for methods such as `trim()`)

	+ ex. `abc123@email.com&nbsp;&nbsp;&nbsp;`   
	will render as:  
	`abc123@email.com`

So if you have a billion spaces after the email address, it will be automatically stripped in an `<input>`. The Demo has your `<input>` wrapped in a `<form>` and the form will actually send data to a real test server. If you send a valid email address with trailing whitespace, look at the response. You'll see that in the response, that the value has no trailing whitespace.

---

Have you considered escaping the periods? 

A `.` means **ANY** one char

A `\.` means a dot or period

It looks like this works:

```
[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{1,4}\S$

```

See tests at [RegEx101](https://regex101.com/r/N967YZ/1/tests)

---

Demo
----

```css
input {
  font: inherit
}
```

```html
<form id='contact' action='https://httpbin.org/post' method='post'>
  <input type="email" id="guestEmail" name="guestEmail" pattern="^[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{1,4}$" required placeholder='abc123@email.com'>
  <input type='submit'>
</form>
```
You are using a HTML `input` field of type `email`. The [defined and documented behavior](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email) of this element is to *accept but remove* any leading or trailing whitespace. If that's not acceptable, don't use this element.

Your attempt at "fixing" the regex are misdirected. Basic regex reading skills should tell you that the apparent original regex only allows alphabetics just before end of string. Your attempts would basically change it from "mostly nearly correct email" to "mostly nearly correct email, followed by any junk at all, as long as it's not whitespace".

Generally speaking, you fix a too-permissive regex by constraining it more (dropping some stuff it would previously accept, perhaps refactoring the regex along the way; for example, to only allow internal dashes in a domain part, you have to split it into first character, optional middle perhaps with dashes, slightly less optional last character without a dash) but certainly not by adding new matching possibilities -- especially not a broad generic character class repeated an arbitrary number of times.

As already noted in comments, there is no way to completely match every valid email address with a regex, but the regular built-in validation in HTML5 is pretty much guaranteed to do a better job than a random regex you found on some PHP forum.