View Full Version : Verifying form fields
Nightfire
07-02-2002, 05:34 PM
How can I verify form fields for different things? Can anyone show me a script?
I'm wanting the username to contain letters and numbers only
password the same, and check that the verify password is the same
email address contains the @
and a textarea that contains no website addresses or email address
Is this possible?
Thanks
joh6nn
07-02-2002, 05:47 PM
function validate() {
var theForm = document.formName;
if ( ! /[a-zA-Z0-9]+/.test(theForm.user.value) ) {
alert('Username may contain only letters or numbers');
return false;
}
if ( ! /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?)$/.test(theForm.email.value) {
alert('email address is invalid');
return false;
}
if ( ( ! /[a-zA-Z0-9]+/.test(theForm.password.value) ) || ( theForm.password.value != theForm.password2.value ) {
alert('Please enter your password again');
return false;
}
if ( /[a-zA-Z0-9]+/.test(theForm.textbox.value) ) {
alert('Text box may not contain email addresses');
return false;
}
if ( /(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(theForm.textbox.value) ) {
alert('Text box may not contain website addresses');
return false;
}
}
that was real fast, off the top of my head. you may have to check that website RE. i found it on some website somewhere, and haven't had a chance to test it myself.
Nightfire
07-02-2002, 06:08 PM
Can you check the form here and see what's wrong? It's saying username is not defined
http://chatterspics.com/register
You only get one attempt with the username though, as it registers straight away.
Thanks
joh6nn
07-02-2002, 06:33 PM
function validate() {
var theForm = document.formName;
you forgot to customize that.
Nightfire
07-02-2002, 07:30 PM
Thanks :) It's working now, sort of.
The one for the textarea, the one to disable websites and email addresses, well if you put a website in, it tells you email addresses can't be used. Can this be fixed easily? If it can't, I'll just modify the alert for the email :)
Thanks again
joh6nn
07-02-2002, 07:37 PM
whoops, made a mistake while i was copying and pasting. change the no email in textbox to the following:
if ( ! /.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?)/.test(theForm.textbox.value) {
alert('Text box may not contain email addresses');
return false;
}
Nightfire
07-02-2002, 07:51 PM
Back. Thanks for your help so far :)
It's now saying that I can't put an email address in, even if I don't put one in or put no text in.
I'd try and figure it out but this means nothing to me.
whammy
07-03-2002, 12:30 AM
Huh? It seems to be working for me... :confused:
<edit>Although I get this error:
Warning: Unable to open 'none' for reading: No such file or directory in /home/chatterspics/public_html/register/index.php on line 188</edit>
Nightfire
07-03-2002, 12:35 AM
Yeah, I took out the email thing.
I've posted my question to that error in the php forum. Everything works ok, just throwing that error for some reason.
joh6nn
07-03-2002, 11:54 AM
once again, i made an error during my copy and paste (sorry, i'm GMT + 1, it was late for me.)
this is right:
if ( /.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?)/.test(theForm.textbox.value) {
alert('Text box may not contain email addresses');
return false;
}
Nightfire
07-03-2002, 02:12 PM
lol thanks Joh6nn :) I'll try it later
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.