- This topic is empty.
-
AuthorPosts
-
October 24, 2011 at 1:07 pm #34892
charlie
ParticipantI’ve set-up a form that uses js validation and sends email with php. In the form fields I have initial values specified such as Name, email, etc. The problem is you can just click the send button and the initial values meet the validation requirements so the email is sent.
Can the initial filed values be made to NOT validate using js or php?
See html form code:
<form action="contact.php" method="post" name="packagingdesignthatsells"><br /> <br /> <p class="form-labels"><span id="sprytextfield2"><span class="textfieldRequiredMsg">Your name is required.</span><input name="name" type="text" class="fields" onfocus="if(this.value == 'Name*') { this.value = ''; }" value="Name*"></span></p><br /> <p class="form-labels"><span id="sprytextfield3"><span class="textfieldRequiredMsg">A valid email is required.</span><input name="email" type="text" class="fields" onfocus="if(this.value == 'Email*') { this.value = ''; }" value="Email*"></span></p><br /> <p class="form-labels"><span id="sprytextfield4"><span class="textfieldRequiredMsg">Your phone number is required.</span><input name="phone" type="text" class="fields" onfocus="if(this.value == 'Phone*') { this.value = ''; }" value="Phone*"> </span></p><br /> <p class="form-labels">Optional<textarea name="message" cols="20" rows="8" class="text-fields" onfocus="this.value=''; this.onfocus=null">Have a particular project in mind?<br /> Give us a few details on how we might help...</textarea></p><br /> <br /> <input name="SEND" type="submit" class="send" value="GET STARTED NOW" ><br /> <input type="hidden" name="Recipient" value="email"><br /> <input type="hidden" name="Subject" value="CONTACT www.URL.com"><br /> <input type="hidden" name="Redirect" value="/thanks.html"><br /> <br /> </form><br />
See PHP (very messy):
<?php<br /> <br /> $thisRecip = $_POST ;<br /> <br /> // $thisRecip = '[email protected]' ;<br /> <br /> $thisEmail = $thisRecip ;<br /> if( !empty($_POST) ) $thisEmail = $_POST ;<br /> <br /> $to = $thisRecip ;<br /> $subject = $_POST ;<br /> $headers = "From: $thisEmail" . "rn" .<br /> 'X-Mailer: PHP/' . phpversion() ;<br /> <br /> $body = "Name = " . $_POST . "n" ;<br /> $body .= "Email = " . $_POST . "n" ;<br /> $body .= "Phone = " . $_POST . "n" ;<br /> $body .= "Message = " . wordwrap($_POST,70) . "n" ;<br /> <br /> <br /> mail($to, $subject, $body, $headers) ;<br /> header('Location:' . $_POST);<br /> <br /> ?><br />
Thank you,
CMP.S.
Wasn’t sure what category to put this question in.October 24, 2011 at 2:34 pm #89542Eric
MemberOctober 24, 2011 at 2:55 pm #89547charlie
ParticipantThanks for the info! I was hoping to avoid adding js to these pages. It seem like overkill.
Could there be a php solution or a way to set the validation to include a requirement that input must contain certain characters or a certain number of characters to validate? (ie. email must contain “@” or phone must contain only numbers)
CM
October 24, 2011 at 3:10 pm #89549standuncan
MemberI am sort of dealing with the same issue here:
https://css-tricks.com/forums/discussion/14225/jquery-placeholder-in-ie-passing-validation…#Item_4You can also see the php validation I put together for the email input here:
https://css-tricks.com/forums/discussion/14161/php-form-validation-example.-secure-or-not-secures#Item_5As far as only numbers, you can search is_numeric on php.net and that should help out.
http://www.php.net/manual/en/function.is-numeric.php
or for how many characters you want look up strlen (string length).October 24, 2011 at 3:54 pm #89551charlie
ParticipantWhat is the validation checking? (just the word “email”):
} else {
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
$errors = 'Invalid email';
}
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
'*+/=?^_`{|}~.-]{0,63})|("[^(\|")]{0,62}"))$",
$local_array[$i])) {
$errors = 'Invalid email';
}
}
Thank you,
This is great!October 24, 2011 at 4:12 pm #89554charlie
ParticipantIs there such an attribute as minlength=””? to use in input field?
October 24, 2011 at 4:47 pm #89561standuncan
MemberThe I think the there is only a HTML5 maxlength attribute, but again if you use it in the php validation you can check against the strlen.
Here is a snippet of the email validation with comments for you:
//Check the form fields<br /> if(empty($email)){<br /> $errors = '<span class="error">Enter your email</span>';<br /> } else{<br /> <br /> // First, we check that there's one @ symbol,<br /> // and that the lengths are right.<br /> if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {<br /> // Email invalid because wrong number of characters<br /> // in one section or wrong number of @ symbols.<br /> $errors = '<span class="error">Invalid email</span>';<br /> }<br /> <br /> // Split it into sections to make life easier<br /> $email_array = explode("@", $email);<br /> $local_array = explode(".", $email_array[0]);<br /> for ($i = 0; $i < sizeof($local_array); $i++) {<br /> <br /> if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&<br /> '*+/=?^_`{|}~.-]{0,63})|("[^(\|")]{0,62}"))$",<br /> $local_array[$i])) {<br /> <br /> $errors = '<span class="error">Invalid email</span>';<br /> }<br /> }<br /> <br /> // Check if domain is IP. If not,<br /> // it should be valid domain name<br /> if (!ereg("^[?[0-9.]+]?$", $email_array[1])) {<br /> <br /> $domain_array = explode(".", $email_array[1]);<br /> <br /> if (sizeof($domain_array) < 2) {<br /> $errors = '<span class="error">Invalid email</span>';<br /> }<br /> <br /> for ($i = 0; $i < sizeof($domain_array); $i++) {<br /> if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|<br /> ([A-Za-z0-9]+))$",<br /> $domain_array[$i])) {<br /> $errors = '<span class="error">Invalid email</span>';<br /> }<br /> }<br /> }<br /> }<br />
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.