/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Confused about using email validation function

I have a form that has the action of “update_question.php”. That php file adds the form data to my database.

However, I want to validate the email address. I have code for that in the form of a function. Can I call that function in the middle of my form? Or should I wait till the form is submitted and call it from “update_question.php”. I’d prefer to call it from my form, but just not sure how to do it.

Thanks!

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@DrMarieauthorMay 28.2009 — I think I found part of my answer but still have a question....

I found this code:

[code=php] 1. function isValidEmail($email){

2. return filter_var(filter_var($email, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);

3. }

4.

5. if(isValidEmail('[email protected]')){

6. //valid

7. } else {

8. //not valid

9. } [/code]


But, how do I define the variable $email from within my form? Or can this only be run in my update_question.php which receives the form?
Copy linkTweet thisAlerts:
@DrMarieauthorMay 28.2009 — Wait...I think I figured it out...I guess it has to be run once the whole form is processed in update.php because I don't have any input until the client submits the form....duh...

Forgive my rambling!


Marie
Copy linkTweet thisAlerts:
@NogDogMay 28.2009 — Glad we could help. ?
Copy linkTweet thisAlerts:
@DrMarieauthorMay 28.2009 — LOL! You guys are so good that you can help without even typing now!

Actually...I'm still a little confused...just reading now about using javascript to validate although I know nothing about java.

I would like to be able to determine that the email is invalid before the client goes through the trouble of filling out the whole form. Is that possible?
Copy linkTweet thisAlerts:
@NogDogMay 28.2009 — Firstly, to be pedantic and avoid confusion, JavaScript is not Java (and vice versa).

I'm sure there must be hundreds of JavaScript email address validation functions out there you can use in your page (of varying qualities, of course). Just remember that (a) there are some people who may not have JavaScript enabled when they access your site, and (b) malicious users can easily bypass any such client-side validation; therefore you will still need to do your server-side validations, as you cannot depend on the browser-side validations to have actually occurred.
Copy linkTweet thisAlerts:
@MindzaiMay 28.2009 — Don't get confused between javascript and java - they are ery different beasts. You can validate using javascript before the form is submitted, but this should be considered a convenience and should not be relied upon, because if it is your sole source of validation it can be bypassed just by disabled javascript. Probably the best way to validate an email in javascript is using a regular expression. To get you started:

[code=html]
<script type="text/javascript">
var email = "[email protected]";
var regex = /^[a-z0-9!#$&#37;&'*+/=?^_{|}~-]+(?:.[a-z0-9!#$%&amp;'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+(?:[a-z]{2,4}|museum|travel)$/i
if (regex.test(email)) {
// email is valid
} else {
// email not valid
}
</script>
[/code]
Copy linkTweet thisAlerts:
@DrMarieauthorMay 28.2009 — thanks guys! I am such a noob I didn't even know Java and Javascript were two separate things!

I think I am understanding things more...I am going to read more about onclick or onsubmit. If I understand right, I should be able to use that to validate with php.

If I go this route, if there is an error and the client has to go back to the form, is it possible to make it so that the info they entered is still there?
Copy linkTweet thisAlerts:
@MindzaiMay 28.2009 — Yes, if you are submitting to the same script of which the form is a part, the $_POST variables will still be set so you can just use them in the form to set defaults:

[code=php]
<?php
// some form processing stuff here

if ($submitted == false || $validated == false):
?>
<form action="foo.php" method="post">
<input type="text" name="bar" value="<?php if (isset($_POST['bar'])) echo $_POST['bar'] ?>" />
</form>
<?php endif ?>
[/code]


This works if your form is separate but included via include() or require() too.

If it's a different script receiving the form for processing, you could use sessions in a similar way to pass the data back.

BTW if you are going to use javascript, do yourself a favour and look into a framework like jQuery, it makes things so much easier, and you can avoid polluting your markup with inline js and onclick and the like.
Copy linkTweet thisAlerts:
@DrMarieauthorMay 28.2009 — I found a great javascript and understood it and implemented it easily...Just though I would post in case other beginners were having this issue.

You can find it at:



Now I will work on the server side validation.
×

Success!

Help @DrMarie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...