HTML5 form validation for email [duplicate] - javascript

This question already has answers here:
HTML5 Email input pattern attribute
(20 answers)
Closed 7 years ago.
I am stuck with this issue to which I did not pay attention to before.
<label> Email Address </label>
<input type="email" id="emailAddress" required placeholder='Email Address' />
<button type="submit" class="button primary small">Submit</button>
In my JS file I am checking for its validation using checkValidity()
checkValidEmail: function(event){
var emailAddress = $('#emailAddress');
if(emailAddress[0].checkValidity()){
console.log('Valid');
} else{
console.log('Not Valid');
}
}
"keyup #emailAddress": "checkValidEmail" // KeyUp works as expected
Output:
'a#b' // Valid
I do not understand this behavior. As per my knowledge #.com was the regex for input email.
Please note: I tried this on multiple sites with forms and it shows the same input. In the above case I am executing this on the chrome browser.
Thanks for taking time to reply!

If you want to use regular expression validation, read on
http://www.regular-expressions.info/email.html
Be aware that below is 100% valid and working email address :-) Use it often for testing incorrectly implemented email validation
xn--80a1acny#xn--80auew.xn--j1amh
Another option is to check if email address domain name is valid. Do do that you can query DNS server for MX record (this one points to SMTP server responsible for receiving incoming mail). You will need server side code to do that.

in HTML5 you can validate email like this
<form>
<input type="email" placeholder="Enter your email">
<input type="submit" value="Submit">
</form>

Related

JQuery can not get the right value by val() function

I have a form like this
$('#email').on('keyup', function() {
console.clear();
console.log($('#email').val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input type="email" name="email" class="form-control" placeholder="Email" required id="email">
and I want to get the value of Email input, so I use this $("#email").val() .
But I find this just working in English not in Chinese.
If I input "33333#去.com" in to the email input, the code will give this value:"33333#xn--1nr.com".
I can not get the point to fix it.This is not utf-8 of unicode.I even not see any of this before.
This shouldn't matter.
xn--1nr.com is the IDNA Punycode encoding for 去.com – after all, if you send mail to 33333#去.com, you will actually end up sending mail to 33333#xn--1nr.com.
The browser is trying to be clever for you by normalizing the value to Punycode since the field is an email field.

Regex on input field for user id syntax [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I have an input field on a registration screen where the user has to enter his user id which is generally 6 characters long. First 2 characters are always alphabets, and the following characters are mostly numbers. (eg. ab123c or xy5678)
How can I check in the input field with jquery / javascript that user only enters in above format before hitting submit.
<input type="text" maxlength="6" name="uid" id="uid" placeholder="enter user id here">
You can validate your input using regex
regex = /^[a-z]{2}[a-z0-9]{4}$/i ;
regex = /^[a-z]{2}[a-z0-9]{4}$/i ;
console.log('ab1234', regex.test('ab1234'));
console.log('abc234', regex.test('abc234'));
console.log('ab1d34', regex.test('ab1d34'));
console.log('ab12e4', regex.test('ab12e4'));
console.log('ab12yz', regex.test('ab12yz'));
console.log('2b1234', regex.test('2b1234'));
console.log('a11234', regex.test('a11234'));
You don't need javascript or jquery. Can use HTML. w3c School
<input type="text" maxlength="6" name="uid" id="uid" placeholder="enter user id here" pattern="[a-zA-z]{2}[a-zA-Z0-9]{4}">
Thanks for the update Barmar, not used it before.
Update:
First I don't know why this is closed. The question isn't about learning regular expression, it's about implementation. Aurelien provides the right answer if you want to use jQuery, mine is if you don't want to use jQuery or javascript. Zohaib ljaz doesn't address the core issue of implementation.
Second: Most of the comments aren't helpful, he does provided examples and it has max-length in the code so of course 6 is the max.
Try this
$('form').on('submit', function(e){
e.preventDefault();
if($('#uid').val().match(/^[a-zA-Z]{2}[a-zA-Z0-9]{4}$/)){
$(this).submit()
}
else {
//Do your error logic here
}
}

Email Validation in Registrraion Form in javascript [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 5 years ago.
I have One Register-form for creating Account.In Email Field How Can i Check Enter Email is Real Email Id or Fake Email Id without Sent Any Email.And Also when User Leave The Field Validation will be start.Any idea please Share me....
You can't do this work with js(without sedn email):
i recommend to send a code to your user rmail and verify that.
this way is good to valid email and register user.
note : if you want to know is the email is in email pattern you can do with html and js :
html way :
<input type="email" name="email" placeholder="Your email">
js way :
How to validate email address in JavaScript?

How can I block html and urls in my input form [duplicate]

This question already has answers here:
Best way to defend against mysql injection and cross site scripting
(4 answers)
Closed 7 years ago.
I have a website that has an input form that submits to a php page and adds a players username to a database and counts the views of that player's name to a top 10 list.
My friend tried out inputting other stuff such as html code and javascript.
it get's displayed on my top 10 list.
do you have any suggestions how I can make my form more secure?
I have been searching for ages and haven't found anything yet.
all help would be highly appreciated :)
<form method="get" action="player.php">
<div class="form-group">
<div class="input-group input-group-lg">
<input name="user" type="text" class="form-control" placeholder="Steve" aria-describedby="sizing-addon2">
<span class="input-group-btn">
<input type="submit" class="btn btn-success" value="View Skin">
</span>
</div>
</form>
Freeze your requirement around username e.g. alphanumeric including special chars and Max length etc. Write validation logic using javascript regular expression. Reject everything else that fails.
Client side (HTML/Javascript) validation is only to be nice and responsive to the user, it can always be circumvented by a malicious user. Any validation needed should be done server side even if it repeats validation done on the client side.
Do not allow user to input special characters is the best way to block it. Well to be more secure and confident always have a limited set of valid characters for each text box and validate them on client-side as well as on the server side.
One example would be
function validateUsername(){
var username = document.getElementById('txt_username').value;
if( /[^a-zA-Z0-9]/.test( username ) ) {
alert('Input is not alphanumeric');
return false;
}
return true;
}
On server side you can also use regular expression. like following
if(preg_match('/[^a-z_\-0-9]/i', $username))
{
echo "not valid string";
}

Send data form to a specific email

I want to send my data form that user type to a specific email. Example:
<form>
<input type="text" placeholder="Your name"/>
<input type="email" placeholder="Your email"/>
<textarea name="content"></textarea>
<input type="button" onclick="sendmail()"/>
</form>
When user clicks the button, an email will be sent to a specific address like admin#admin.com with email's content is what user type in the form. Can we reach that result with only JavaScript or jQuery?
You can't do it without additional modules :
This is a good tutorial , you could create a dedicated address reported#yoursite.com and have it email your admin address when a user registers
<a id="emailLnk" >send mail</a>
<script type="text/javascript">
$(document).ready(function() {
$("#emailLnk").click(function(){
document.location.href = "mailto:xyz#something.com";
});
});​
</script>
No, unless someone took the time to rewrite a SMTP client or sendmail in JavaScript, which is not the case. Moreover, that would mean giving away the SMTP password (which is bad) or not using a SMTP at all, meaning that almost all free/commercial providers will reject your e-mail.
You should set up a minimal PHP script to receive a POST request and forward the body as e-mail to your designed account. You should be able to do it in a handful of lines.

Categories