0

I am very new to coding so go easy.

I am trying to make a email validation form but it needs to reject a blank cell (input box) sorry for being so bad at coding..... i also was going to use a regex

it has to be alpanumeric@alpanumeric.alpanumeric

sorry

Ryan
  • 21
  • 3
  • 4
    Why would you use a regex to detect an empty value? And what did you try, where are you even trying to inject it? Your code misses just about every bit of detail required to even consider writing a decent answer. – Niels Keurentjes Nov 05 '14 at 16:44
  • Regular expressions are used to find specific words inside other words. If you just want to check if the input is empty, compare its value against an empty string. – Markai Nov 05 '14 at 16:46
  • Search "checking for empty inputs with javascript" or "validating inputs html5". You dont need to apologize for being bad at coding, just post the code you are working on and make a valiant effort and the community will help. – JayD Nov 05 '14 at 16:49

3 Answers3

0

The correct behavior in this case would be to perform a "pre-check" on fields before actually executing some more complex validation (eg: regular expressions).

The logic would look something like this:

valid_email = false;
email = strip_leading_trailing_spaces( email ); // don't forget to cleanup user input
if ( email != "" ) {
  // perform regex testing here, set valid_email to false if failed
}

// handle "valid_email" variable here

It's worth noting here that any client side validation should be duplicated to/re-checked on the server as any user with a little knowledge in JS could easily bypass any validation done on the clients computer.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • Good thinking to point this out, especially if you consider using it for account/session management etc. A lot of people seem not to be aware of this – Markai Nov 05 '14 at 16:52
  • You can use regex to test empty string. – Salman A Nov 05 '14 at 16:53
  • @SalmanA - correct, but why would you run a regular expression on a string that you already know is empty? – Lix Nov 05 '14 at 16:54
0

You don't need a regex if you are just checking to see if it's empty:

<input type=text id=email><button onClick="validate()">Validate</button>

<script language="javascript">
  function validate() {
    if ($("#email").val().length == 0) {
      alert("Enter an email address");
    }
  }
</script>
sandlb
  • 198
  • 2
  • 9
  • 2
    Please don't give jQuery-dependent answers on generic Javascript questions. I'm not downvoting since your answer is essentially right, but please just use `document.getElementById('email').value` next time. – Niels Keurentjes Nov 05 '14 at 17:07
-1

with a regexp :

var valid_email = ! email.match( /^\s*$/ ) ;

If there's only spaces and tabs or nothing then valid_email = false

demo : http://regex101.com/r/iX8lF7/1

Sly
  • 361
  • 2
  • 9