kcelsi Posted January 10, 2014 Share Posted January 10, 2014 (edited) Hello, I'm creating a form using code I obtained from an online tutorial. It works perfectly and validates as it should. I would like to add a "phone" field which would also need validation. So far I have not been able to get the field to validate. What am I missing?? I've included the parts of the php that I changed and also attached my javascript validation file as a text file. Here is a link to the page the form is on: http://www.idealwindow.com/Contact_Marketing.php Thanks for any help you can offer. In the head tag: <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools-yui-compressed.js"></script> <script type="text/javascript" src="js/validation.js"></script> <script type="text/javascript"> var nameError = '<?php echo $error_messages['fullname']; ?>'; var phoneError = '<?php echo $error_messages['phone']; ?>'; var emailError = '<?php echo $error_messages['email']; ?>'; var commentError = '<?php echo $error_messages['comment']; ?>'; function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}} } </script> In the body tag: <div class="row"> <div class="label">Full Name</div><!-- end .label --> <div class="input"> <input type="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?> </div><!-- end .input --> <div class="context">e.g. John Smith or Jane Doe</div><!-- end .context --> </div><!-- end .row --> <div class="row"> <div class="label">Phone</div><!-- end .label --> <div class="input"> <input type="text" id="phone" class="detail" name="phone" value="<?php echo isset($_POST['phone'])? $_POST['phone'] : ''; ?>" /><?php if(in_array('phone', $validation)): ?><span class="error"><?php echo $error_messages['phone']; ?></span><?php endif; ?> </div><!-- end .input --> <div class="context">e.g. 000-000-0000</div><!-- end .context --> </div><!-- end .row --> validation.txt Edited January 10, 2014 by kcelsi Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Could you just show us you attempt at the validation and NOT all that meaningless JS and outside code? Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 (edited) Sorry, I'm new to php and am learning as I go. Can you tell me exactly which part you need? I'm not sure what "attempt at validation" means. Edited January 10, 2014 by kcelsi Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Show us the part where you are checking the phone number and trying to validate it? That part. Or haven't you tried to look at this code yet? Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 Yes, I've looked at the code thoroughly and I'm trying to decipher it. I thought this part did the validation: if (phone.get('value').length === 0) { isValid = false; addError(phone, phoneError); } else { isValid = true; removeError(phone); } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Assuming that code is some JS, what seems to be the problem, other than all you do is check if something is present.? Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 I need to have the field display an error if the field is not filled out. If you go to the link I included, you will see that if you hit Send Message without filling in the form, the name, email and message fields will display the error, but not the phone field. Please be patient with me. I really need to learn this. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 1 - I don't go to outside links. Ever. Just a phobia I have. 2 - You mentioned that you were new to php, but you are asking about a javascript function. Which language do you have the question about, since this is a php forum. Suggestion - put an alert or two in the js code that you showed us and make sure that you are actually getting to that code and what the code is doing. If the problem is not in the JS code you gave us, then perhaps you want to show us the pertinent php code? Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 I figured out the problem. There was an external php include that I forgot to update. The field is validating fine now. Thanks for your help and patience. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 you're welcome. Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 (edited) One more question about this... How can I edit this code so that the phone number field will only be validated if it is in the form of "000-000-0000"? <?php // Set email variables $email_to = 'kcelsi@littlechisel.com'; $email_subject = 'Form submission'; // Set required fields $required_fields = array('fullname','phone','email','comment'); // set error messages $error_messages = array( 'fullname' => 'Please enter a Name to proceed.', 'phone' => 'Please enter a Phone Number to proceed.', 'email' => 'Please enter a valid Email Address to continue.', 'comment' => 'Please enter your Message to continue.' ); // Set form status $form_complete = FALSE; // configure validation array $validation = array(); // check form submittal if(!empty($_POST)) { // Sanitise POST array foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value)); // Loop into required fields and make sure they match our needs foreach($required_fields as $field) { // the field has been submitted? if(!array_key_exists($field, $_POST)) array_push($validation, $field); // check there is information in the field? if($_POST[$field] == '') array_push($validation, $field); // validate the email address supplied if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field); } // basic validation result if(count($validation) == 0) { // Prepare our content string $email_content = 'New Website Comment: ' . "\n\n"; // simple email content foreach($_POST as $key => $value) { if($key != 'submit') $email_content .= $key . ': ' . $value . "\n"; } // if validation passed ok then send the email mail($email_to, $email_subject, $email_content); // Update form switch $form_complete = TRUE; } } function validate_email_address($email = FALSE) { return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE; } function remove_email_injection($field = FALSE) { return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field)); } ?> Edited January 10, 2014 by kcelsi Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Once again - you've posted too much code. Not interested in all that other stuff - looks like you'll have to dig it out. Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 It's difficult for me to dig out exactly which parts need to be changed, but I'm guessing that I have to modify one of these to pertain to phone instead of email...and maybe set a $phone variable??? function validate_email_address($email = FALSE) { return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE; ) function remove_email_injection($field = FALSE) { return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field)); } Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 I have found a beginner php forum that will answer my questions without getting annoyed. Sorry to have bothered you. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Good luck with that site. I wasn't annoyed - simply conveying to you that good posting should only include enough code to help the readers see your problem and help you to solve it. Not so much that they have to read a book to make sense of it. And actually - this site is pretty much a beginner forum as well...... Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 I understand about good posting and I only want to put up what is useful. The problem with learning programming is that there's so much code whether in the head, body, external files, etc that I have a hard time figuring out what is relevant. If you had instructed me about which part I should have used, then in future posts I would have known what to do. There's a big learning curve here. In my opinion, you should have taken a little extra time to read the code I posted this time, answered my question with some suggested solutions and then stated that in future posts, I should try not to put in things that are not necessary if possible. It just would have been a nice courtesy. Sorry, I'm just frustrated that I haven't figured this out yet. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Actually if you are just presenting code you took from someplace else, how do you call that learning? NO - We are here to help you with YOUR code. We are not here to read somebody else's long posting that you have decided to use without even knowing what it does or how it works. If you haven't learned enough to take on the task you've chosen, then why should some volunteer (us) be asked to do it for you? Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 Isn't that what you are supposed to do? Help beginners with php? All I want to do is find out how to validate a phone number in a certain format. After all of this, you still haven't suggested to me one line of code that could accomplish that. Why are you judging the method that I use to learn? If someone else writes good code, why can't I use it for my own projects? How can I start to write my own code, if I don't learn from seeing other examples first? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 If you have a good example of a phone validation, why aren't you using it? Oh - you want us to write it for you. What kind of validation do you want? 1 - test the value for a numeric only value; 2 - test for a valid length - depends on your expected inputs I suppose. I'd check for 7 or 10 myself. 3 - test for a 10 digit number that doesn't begin with 0 or 1; (BTW - I would only accept numbers without formatting in my input fields. Formatting can be added when I display it.) Ok - now that's the algorithm that I would use - you code it up. Of course if that's not the algorithm that works for your area, then ignore it. Quote Link to comment Share on other sites More sharing options...
kcelsi Posted January 10, 2014 Author Share Posted January 10, 2014 I'm afraid that my desired answer to you is not appropriate to post in a forum so I'll offer the subdued version. Why don't you stick to writing your own code and stay out of volunteer jobs designed to help people. You are obviously not cut out for it and your pompous attitude could use some checking. I'm sorry I wasted so many minutes of my day even discussing this with you. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 I'm sorry you feel that way. I'm also sorry that you don't see that I am actually trying to help you learn php by asking you to write some code. Apparently you need someone else to write your code and another person to interpret your code and another person to add code to it that the first person forgot to include for you. Good luck young man. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.