![]() |
Javascript validation form
Hi, I'm trying to validate a form using the javascript tutorial here.
There is an example here, but I can't seem to duplicate the results, not sure why. Code:
<script language="JavaScript">Code:
<form name="contact_form" id="contact_form" method=post action="contact_submit.php"> |
Quote:
Replace this:- Code:
function checkForm()Code:
function checkForm() {and replace <script language="JavaScript"> by <script type = "text/javascript"> Your email validation regex is somewhat over-complicated - I would suggest:- var regex = /^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/ There is really no point in over-complicated validation of email adddresses, telephone numbers and so forth, as nothing can prevent the most obvious and common source of errors, that is simple mistyping e.g. philip@myadddress.com. It is better to request that the user inputs his email address twice by way of confirmation, and check that the two match. It is your responsibility to die() if necessary….. - PHP Manual |
I don't get it. I created a test page and put the whole pages code on that after i edited it as philip suggested (thanks!), then deleted everything but the javascript and the form to see if it was something else influencing it. and it clearly wasn't since it still submitted.
It works just fine in this bit... Code:
<html> |
Please say exactly what the problem is, and what error messages (if any) you receive. It is not much use posting the code which does work - what is it that you think does not work?
I should say that having made the alterations which I suggested the form is working fine for me, although there is no validation of the phone number which is supposed to be a required field. I have just noticed another defect:- <td align="left" valign="top"> <textarea name="comments" id="comments" cols="30" rows="8" wrap="VIRTUAL" style='width: 250px; font-family: Tahoma; font-size: 9pt;'></textarea> Replace "comments" by "message" - which is the id reference in the script. An improvement would be to replace all these tests:- if (trim(cname.value) == '') // a single character such as * will pass the test by if(trim(cname.value.length) < 6) // minimum 6 characters or whatever is felt appropriate in each case |
Hint: run it in Firefox and look at Tools > Error Console.
|
Sorry for the confusion. The last bit of javascript I provided was the sample tutorial that I'm working from. Everything there works. I'm trying to incorporate that into my own website. I'm not getting any errors at all. That's the problem. I'm supposed to receive a javascript popup, and I'm not getting one. The html form is the same as I posted earlier, but I did make the comments/message fix (thanks!). I also updated the javascript and I'm still not getting the popup message.
The phone number also needs a validation but I've never actually coded much JS, so I'm not sure how to go about it... The error console tells me that on line 29 (of the whole page that is) "return not in function". Code:
<script type="text/javascript"> |
The 'return not in function' is because there's a closing brace that shouldn't be there, on the line before your first if.
|
I don't know what else I could possibly do to make this thing work. I've fixed that problem, and I'm not getting any more errors in the error console anymore, so that's good at least.
|
Well, you've got some more breakage in that you're doing
Code:
document.getElementById("name");Nothing shows up in the error console after you click the submit button? That seems hard to believe. |
ah ha, AFTER I submit the form. now there's my problem. *bangs head against wall*
My copy here that I'm working with shows "name" as the ID, so maybe you're looking at the tutorial copy I posted? I am getting a str.replace is not a function error now on line 71, which is... Code:
return str.replace(/^\s+|\s+$/g,''); |
What that really means is that str isn't what you think it is. And well it might not be... you're saying in your various if/elses things like:
Code:
trim(cname.value.length)Code:
trim(cname.value).length |
Works like a charm, thanks so much chaos! How should I verify the phone number?
|
You're welcome. There's a forum thanks feature too and all. :)
If you're comfortable requiring that the phone number be precisely NNN-NNN-NNNN format, you could use a regex like whateverstr.match(/^\d{3}-\d{3}-\d{4}$/). Complexity increases the more forgiving you want it to be. |
thanked. :thumbsup: So what would be the string to support dashes, spaces, OR nothing at all between the numbers? Is this to complicated to ask for help with?
|
Nah, 'sfine. That'd be /^\d{3}[- ]?\d{3}[- ]?\d{4}$/.
|
| All times are GMT +1. The time now is 10:37 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.