Click to See Complete Forum and Search --> : JavaScript question, your help needed Please!!!


DanUK
03-10-2003, 07:13 AM
Hi everyone,
I know this will sound like a silly question to you, but I'm new to this and I've tried hard, but everything I'm trying from other scripts etc won't work. Basically from the code I will paste below, I want to make it so that only normal characters can be accepted in the fields (A-Z,a-z and 0-9, nothing else). Can you please tell me what I need to add to the following code?

Here is my HEAD stuff (body is below):

<script language="javascript"><!--

function simpleCheck(form) {

if (form.contactNAME.value == "") {
alert("Please supply your name.");
form.contactNAME.select(); }

else if (form.contactEMAIL.value =="") {
alert("Please include your e-mail address.");
form.contactEMAIL.select(); }

else {
form.method="POST";
form.target="_self"
form.submit();
}

}
//--></script>


and lastly, body:
(as well as my normal info for the submit)

<form onSubmit="simpleCheck(this); return false;"(and then my other info for submission).

All this works fine, but can you please tell me how to make it accept only those/

Many thanks in advance for your help!

DanUK
03-10-2003, 07:21 AM
thanks!!!

Dave can i be really cheeky and ask how to make sure its a valid email, in form of user@host.com ? to stop abuse ... thanks again for your quick response !!!

DanUK
03-10-2003, 07:29 AM
thanks again dave, works great.

just one quick qu abotu this one:

you can use this:

if(!/^\w+$/i.test(form.contactNAME.value) {


It works fine with everything apart from a _ so I put it to

if(!/^_\w+$/i.test(form.contactNAME.value) {

It works ok if there's just one _, but if there's more it will be ok..any ideas?

thanks again! *my last post i hope, i apprecaite ur help!*

DanUK
03-10-2003, 07:37 AM
yes it does allow the normal A-Z and a-z and 0-9, that's all perfect, it ignores all the 'weird' characters, but how can i also make it disallow _, i need it to just accept A-Z,a-z and 0-9? Or is it not possible?

For some strange reason the e-mail one isn't working at all now, this is the code for the e-mail bit:

else if (!/^\w+@\w+\.[a-z]{2,3}$/i(form.replyEMAIL.value)) {
alert("Please include your well-formed email address.");
form.replyEMAIL.select(); }


thanks again.

DanUK
03-10-2003, 07:45 AM
works perfect, thank you so much !!! Your help is much appreciated.
One last question, if i may. For example with the contactNAME one, is there a way to bring up a seperate alert, as I have one for just to make sure you enter a name, is there a way to add another just in the case there is disallowed characters, so its more clear? If not that's fine! thanks again, you've saved my site from so much abuse !

DanUK
03-10-2003, 08:22 AM
hi my last post i promise hehe.
with the e-mail one, it works great, just a slight prob, it will only accept user@host.com .. if someone has a mail like user@host.org.uk it will not accept it because of the two .'s, any ideas?
Also, unless its just my browser, it seems to only accept lower case in the mail. thanks again so much

DanUK
03-10-2003, 03:36 PM
Hi Sorry to bother you again, I know you are all busy, and I really promise this is my last question hehehe.

Can anyone tell me why with the following code:

else if (!/^\w+@\w+\.[a-z]{2,3}$/i.test(form.contactEMAIL.value)) {
alert("Please include your e-mail address. It should be in the format of you@host.com.");
form.contactEMAIL.select(); }

it will only allow someone@host.com, it wont allow say, someone@host.org.uk, how can i let it add a further . for people who use things like co.uk, or org.uk etc? Also unless it's just my browser, it seems I can only input lower text, this may be my browser though. Any help you can give me will be much appreciated !!! Thanks!

Nedals
03-10-2003, 03:54 PM
var re = /^\w+@\w+\.[a-z]{2,3}(\.[a-z]{2})?$/i;
if (re.test(email)) { alert('ok') } else { alert('invalid') }

Add... (\.[a-z]{2})? ('?' is 0 or 1 time )

khalidali63
03-10-2003, 03:55 PM
Well the code snippet in question does not validate most of the top level domains.

Try this,it might be something that answer your question.

http://68.145.35.86/skills/javascripts/EmailValidator.html

Cheers

Khalid

DanUK
03-10-2003, 04:08 PM
hi thanks for your help.

Unfortunately it still didnt work ...
from your reply I put this:

else if (!/^\w+@\w+\.[a-z]{2,3}(\.[a-z]{2})?$/i.test(form.contactEMAIL.value)) {
alert("Please include your e-mail address. It should be in the format of you@host.com.");
form.contactEMAIL.select(); }

Any ideas how I can get this working are much appreciated, many thanks! Basically so it just always whatever@whatever (with however many dots, too many domains that I don't know about either) heehe. Thanks again !

khalidali63
03-10-2003, 04:26 PM
Originally posted by skydan
...
Unfortunately it still didnt work ...


I am not sure what did u do,
here is the email which I tested on the link I provided earlier to you

someone@host.org.uk

and it is validated.Isn't that you wanted?

Khalid

DanUK
03-10-2003, 04:29 PM
I went to that link but it didnt do what I wanted. Basically the code I pasted in my last post, I just want it so that it allows whatever@whatever.com.uk or whatever, which it doesnt at the moment. It only allows whatever@whatever.com. I just need to know what part of the code to change so it allows that, so the user can submit on my contact page.

thanks

khalidali63
03-10-2003, 04:32 PM
see if pyro is available,he is very good with regexp and may be he can do it for you.

Khalid

Nedals
03-10-2003, 04:51 PM
The code I posted works as desired.. Cut, paste and try it setting email = "name@domain.com.uk";
Make life easy for your self and create a new variable

var re = /^\w+@\w+\.[a-z]{2,3}(\.[a-z]{2})?$/i;

your code then is...

else if (!re.test(form.contactEMAIL.value)) {

a lot easier to read and DEBUG. :)

Is this 'form.contactEMAIL.value' correct. 'form' seems a little strange. Should this not be...

document.formName.contactEMAIL.value??

DanUK
03-10-2003, 05:04 PM
brilliant thanks I made a typo ! Thank you so much again !!!.

I hope I'm not being cheeky,if I am please ignore me... if this is my script for my contact page:

<script language="javascript"><!--

function simpleCheck(form) {

if (!/^[a-z0-9]+$/i.test(form.contactNAME.value)) {
alert("Please supply your name. If it contains any other form of text (other than A-Z, a-z or 0-9) please remove it.");
form.contactNAME.select(); }

else if (!/^\w+@\w+\.[a-z]{2,3}$/i.test(form.contactEMAIL.value)) {
alert("Please include your e-mail address. It should be in the format of you@host.com.");
form.contactEMAIL.select(); }

else {
form.method="POST";
form.target="_self"
form.submit();
}

}
//--></script>


is there a way a make to just make the error messages come up at the end, in one window? at the moment they come up seperate? I have another form (much longer) and it gets kinda annoying with them popping up all teh time. If it's not possible, that's fine. Once again thanks for your help.

Nedals
03-10-2003, 05:20 PM
function simpleCheck(form) {
var errMsg = "";
if (!/^[a-z0-9]+$/i.test(form.contactNAME.value)) {
errMsg = "Please supply your name. If it contains any other form of text (other than A-Z, a-z or 0-9) please remove it.\n";
form.contactNAME.select();
}

else if (!/^\w+@\w+\.[a-z]{2,3}$/i.test(form.contactEMAIL.value)) {
errMsg += "Please include your e-mail address. It should be in the format of you@host.com.";
form.contactEMAIL.select(); }

else {
if (errMsg) alert(errMsg);
form.method="POST";
form.target="_self"
form.submit();
}
Like that? :)

Update
Took out code tags, (too wide) and fixed typo!

DanUK
03-10-2003, 06:48 PM
just comes up with Errors on page, hmm ?

DanUK
03-10-2003, 07:29 PM
why when I'm use this code I get 'Errors on page'? Anyone can help? thanks!

function simpleCheck(form) {
var errMsg = "";
if (!/^[a-z0-9]+$/i.test(form.contactNAME.value)) {
errMsg = "Please supply your name. If it contains any other form of text (other than A-Z, a-z or 0-9) please remove it.\n";
form.contactNAME.select();
}

else if (!/^\w+@\w+\.[a-z]{2,3}$/i.test(form.contactEMAIL.value)) {
errMsg += "Please include your e-mail address. It should be in the format of you@host.com.";
form.contactEMAIL.select(); }

else {
if (errMsg) alert(errMsg);
form.method="POST";
form.target="_self"
form.submit();
}

pyro
03-10-2003, 08:06 PM
You may want to change this RegExp /^\w+@\w+\.[a-z]{2,3}$/i to something more like this /^\w+@\w+\.[a-z]{2,4}$/i to include top level domains such as .info or .name. The longest top level domain that I know of (there may be a longer) is .museum. You may even want something more like /^\w+@\w+\.[a-z]{2,6}$/i