Rectangle 27 6

Client Side validation can always be bypassed. There's no way around this. Backend validation should always be used when dealing with sensitive information, writing to/reading from a db, or really anything that could be hacked.

Client side validation is just there because it's faster. Much rather know instantly that my email address wasn't formatted correctly than after I've submitted the form, the server checked it out, and sent something back.

Is Client-side JavaScript / jQuery form validation useless? - Stack Ov...

javascript jquery validation
Rectangle 27 6

Client Side validation can always be bypassed. There's no way around this. Backend validation should always be used when dealing with sensitive information, writing to/reading from a db, or really anything that could be hacked.

Client side validation is just there because it's faster. Much rather know instantly that my email address wasn't formatted correctly than after I've submitted the form, the server checked it out, and sent something back.

Is Client-side JavaScript / jQuery form validation useless? - Stack Ov...

javascript jquery validation
Rectangle 27 198

$("input").prop('required',true);

Hmm, added a submit button but still not validating or adding the required attribute to the input fields. jsfiddle.net/japaneselanguagefriend/LEZ4r

if 'input' is an id the jquery is missing the # in the selector here.

TypeError: element.prop is not a function

javascript - jQuery add required to input fields - Stack Overflow

javascript jquery validation
Rectangle 27 3322

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

function validateEmail(email) {
    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,}))$/;
    return re.test(email.toLowerCase());
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

function validateEmail(email) {
  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,}))$/;
  return re.test(email);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.

@Randall: Can you give an example of an email address that this won't let through?

@GoodPerson I just tried to email n@ai to tell him/her they have a cool email address. But alas, gmail wouldn't let me. I suspect whoever that is has bigger problems communicating with others via email than just my site's javascript validation! But thanks for rising to the challenge.

In my opinion it's a good thing to reject ridiculous email addresses with escaped `@' signs and such. If some Mr L@me wants to use my sites, he will have to get a sensible email address first.

For all the people commenting that this is "good enough": look, you're simply thinking about this problem wrong. That's OK. It's a choice you can make for your users. I ain't mad at it. But, you know, technically speaking, you're demonstratively, provably wrong.

regex - How can you validate an email address in JavaScript? - Stack O...

javascript regex validation email email-validation
Rectangle 27 3290

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

function validateEmail(email) {
    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,}))$/;
    return re.test(email.toLowerCase());
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

function validateEmail(email) {
  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,}))$/;
  return re.test(email);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.

@Randall: Can you give an example of an email address that this won't let through?

@GoodPerson I just tried to email n@ai to tell him/her they have a cool email address. But alas, gmail wouldn't let me. I suspect whoever that is has bigger problems communicating with others via email than just my site's javascript validation! But thanks for rising to the challenge.

In my opinion it's a good thing to reject ridiculous email addresses with escaped `@' signs and such. If some Mr L@me wants to use my sites, he will have to get a sensible email address first.

For all the people commenting that this is "good enough": look, you're simply thinking about this problem wrong. That's OK. It's a choice you can make for your users. I ain't mad at it. But, you know, technically speaking, you're demonstratively, provably wrong.

regex - How to validate email address in JavaScript? - Stack Overflow

javascript regex validation email email-validation
Rectangle 27 3284

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

function validateEmail(email) {
    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,}))$/;
    return re.test(email.toLowerCase());
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

function validateEmail(email) {
  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,}))$/;
  return re.test(email);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.

@Randall: Can you give an example of an email address that this won't let through?

@GoodPerson I just tried to email n@ai to tell him/her they have a cool email address. But alas, gmail wouldn't let me. I suspect whoever that is has bigger problems communicating with others via email than just my site's javascript validation! But thanks for rising to the challenge.

In my opinion it's a good thing to reject ridiculous email addresses with escaped `@' signs and such. If some Mr L@me wants to use my sites, he will have to get a sensible email address first.

For all the people commenting that this is "good enough": look, you're simply thinking about this problem wrong. That's OK. It's a choice you can make for your users. I ain't mad at it. But, you know, technically speaking, you're demonstratively, provably wrong.

regex - How to validate email address in JavaScript? - Stack Overflow

javascript regex validation email email-validation
Rectangle 27 3210

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

function validateEmail(email) {
    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,}))$/;
    return re.test(email);
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

function validateEmail(email) {
  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,}))$/;
  return re.test(email);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.

@Randall: Can you give an example of an email address that this won't let through?

@GoodPerson I just tried to email n@ai to tell him/her they have a cool email address. But alas, gmail wouldn't let me. I suspect whoever that is has bigger problems communicating with others via email than just my site's javascript validation! But thanks for rising to the challenge.

In my opinion it's a good thing to reject ridiculous email addresses with escaped `@' signs and such. If some Mr L@me wants to use my sites, he will have to get a sensible email address first.

For all the people commenting that this is "good enough": look, you're simply thinking about this problem wrong. That's OK. It's a choice you can make for your users. I ain't mad at it. But, you know, technically speaking, you're demonstratively, provably wrong.

regex - How to validate email address in JavaScript? - Stack Overflow

javascript regex validation email email-validation
Rectangle 27 3202

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

function validateEmail(email) {
    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,}))$/;
    return re.test(email);
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

function validateEmail(email) {
  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,}))$/;
  return re.test(email);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.

@Randall: Can you give an example of an email address that this won't let through?

@GoodPerson I just tried to email n@ai to tell him/her they have a cool email address. But alas, gmail wouldn't let me. I suspect whoever that is has bigger problems communicating with others via email than just my site's javascript validation! But thanks for rising to the challenge.

In my opinion it's a good thing to reject ridiculous email addresses with escaped `@' signs and such. If some Mr L@me wants to use my sites, he will have to get a sensible email address first.

For all the people commenting that this is "good enough": look, you're simply thinking about this problem wrong. That's OK. It's a choice you can make for your users. I ain't mad at it. But, you know, technically speaking, you're demonstratively, provably wrong.

regex - How to validate email address in JavaScript? - Stack Overflow

javascript regex validation email email-validation
Rectangle 27 3200

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

function validateEmail(email) {
    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,}))$/;
    return re.test(email);
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

function validateEmail(email) {
  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,}))$/;
  return re.test(email);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.

@Randall: Can you give an example of an email address that this won't let through?

-1 you will never match all valid email addresses with a regular expression

@GoodPerson I just tried to email n@ai to tell him/her they have a cool email address. But alas, gmail wouldn't let me. I suspect whoever that is has bigger problems communicating with others via email than just my site's javascript validation! But thanks for rising to the challenge.

For all the people commenting that this is "good enough": look, you're simply thinking about this problem wrong. That's OK. It's a choice you can make for your users. I ain't mad at it. But, you know, technically speaking, you're demonstratively, provably wrong.

regex - How to validate email address in JavaScript? - Stack Overflow

javascript regex validation email email-validation
Rectangle 27 3

One word: usability. You shouldn't only write you web apps in defense against hackers. There are lots more regular users out there who just want easy-to-use, intuitive software.

Is Client-side JavaScript / jQuery form validation useless? - Stack Ov...

javascript jquery validation
Rectangle 27 3

One word: usability. You shouldn't only write you web apps in defense against hackers. There are lots more regular users out there who just want easy-to-use, intuitive software.

Is Client-side JavaScript / jQuery form validation useless? - Stack Ov...

javascript jquery validation
Rectangle 27 43

You can use the input (FF) and propertychange (all others) events to catch all forms of input including keyboard and rmb cut paste.

$('input').bind('input propertychange', function() {
    $('#output').html($(this).val());
});

This is the best answer I found so far, and I wrote this example based on it. Worked great on FF, Chrome and Safari, kinda worked on IE6 (got a non-fatal stack overflow sometimes, but who cares about IE6), but failed on Opera (and Konqueror, but I don't care much about it either). So I'm leaving the question open for some more time...

@mgibsonbr - seems to work in Opera 11.61 for me. What exactly does this example do?

It attempts to only let the user enter valid numbers, but since this is impossible (while the user is still typing, the value will be invalid), it also accept valid prefixes (showing a red border if the whole text is not valid). So, if the user attempts to add or delete characters that will make the input invalid, it reverts to the last saved value. Otherwise, it saves the new value. (Not necessarily something I'd do in a real system, though - see nnnnnn's answer) The example fails on Opera 10.63, but since it works on newer versions, it's fine!

javascript - Validate html text input as it's typed - Stack Overflow

javascript jquery validation
Rectangle 27 43

You can use the input (FF) and propertychange (all others) events to catch all forms of input including keyboard and rmb cut paste.

$('input').bind('input propertychange', function() {
    $('#output').html($(this).val());
});

This is the best answer I found so far, and I wrote this example based on it. Worked great on FF, Chrome and Safari, kinda worked on IE6 (got a non-fatal stack overflow sometimes, but who cares about IE6), but failed on Opera (and Konqueror, but I don't care much about it either). So I'm leaving the question open for some more time...

@mgibsonbr - seems to work in Opera 11.61 for me. What exactly does this example do?

It attempts to only let the user enter valid numbers, but since this is impossible (while the user is still typing, the value will be invalid), it also accept valid prefixes (showing a red border if the whole text is not valid). So, if the user attempts to add or delete characters that will make the input invalid, it reverts to the last saved value. Otherwise, it saves the new value. (Not necessarily something I'd do in a real system, though - see nnnnnn's answer) The example fails on Opera 10.63, but since it works on newer versions, it's fine!

javascript - Validate html text input as it's typed - Stack Overflow

javascript jquery validation
Rectangle 27 126

(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
(/^
(?=.*\d)                //should contain at least one digit
(?=.*[a-z])             //should contain at least one lower case
(?=.*[A-Z])             //should contain at least one upper case
[a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters
$/)

This is not doing what the OP (mistakenly, in my opinion) is asking for. It allows other characters than alphanumerics.

(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/) if the user can enter symbols and spaces - allows for a more secure password

jquery - javascript regex for password containing at least 8 character...

javascript jquery regex validation
Rectangle 27 126

(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
(/^
(?=.*\d)                //should contain at least one digit
(?=.*[a-z])             //should contain at least one lower case
(?=.*[A-Z])             //should contain at least one upper case
[a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters
$/)

This is not doing what the OP (mistakenly, in my opinion) is asking for. It allows other characters than alphanumerics.

(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/) if the user can enter symbols and spaces - allows for a more secure password

jquery - javascript regex for password containing at least 8 character...

javascript jquery regex validation
Rectangle 27 123

(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
(/^
(?=.*\d)                //should contain at least one digit
(?=.*[a-z])             //should contain at least one lower case
(?=.*[A-Z])             //should contain at least one upper case
[a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters
$/)

This is not doing what the OP (mistakenly, in my opinion) is asking for. It allows other characters than alphanumerics.

(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/) if the user can enter symbols and spaces - allows for a more secure password

jquery - javascript regex for password containing at least 8 character...

javascript jquery regex validation
Rectangle 27 2

It provides better user experience. That's the point. Instead of submitting the form every time and waiting for server response user can see the results of validation immediately after changing form values.

Is Client-side JavaScript / jQuery form validation useless? - Stack Ov...

javascript jquery validation
Rectangle 27 2

It provides better user experience. That's the point. Instead of submitting the form every time and waiting for server response user can see the results of validation immediately after changing form values.

Is Client-side JavaScript / jQuery form validation useless? - Stack Ov...

javascript jquery validation
Rectangle 27 30

You can do it by using attr, the mistake that you made is that you put the true inside quotes. instead of that try this:

$("input").attr("required", true);

if 'input' is an id the jquery is missing the # in the selector here.

@JohnMeyer "input" is the name of a tag selector

javascript - jQuery add required to input fields - Stack Overflow

javascript jquery validation
Rectangle 27 7

Just posting for anyone else getting here from a search...

The reason you were getting the error when loading the files in the head is that jquery.validate.unobtrusive requires jquery.validate. That's what was causing the error.

(ie, double-check that you're successfully including jquery.validate before jquery.validate.unobtrusive)

jquery - unobtrusive validation javascript error on load - $jQval is u...

javascript jquery asp.net-mvc-3 unobtrusive-javascript unobtrusive-validation