Checking if a certain character has been entered

How would you check if a certain character has been entered in a form field? An example would be an e-mail form…I want to check if it has the ‘@’ character. What command would you use? thanks

Btw, just give me the command and not the whole code for now so i can try it out myself :stuck_out_tongue:

I cheat and use the Form Validation behavior by Yaromat. I don’t bother trying to reinvent the wheel, and I HATE coding in Javascript. :}

Dreamweaver Extensions at www.yaromat.com

Yeah that would be good for a last resort :stuck_out_tongue: but i want to learn this so any other ideas?!

You’ll want to do it client-side, before the form actually gets submitted to the server, so Javascript is the best way to go. You could do it server side, after submit, then return to the form if invalid, but that’s clumsy for the user.

DigitalPimp is the resident Javascript whiz, when I checks in he should be able to at least point you in the right direction. I can code ASP script in my sleep, but I only know enough Javascript to be dangerous. =)

For checking whether there is an @ in a string with JS…


if(yourEmailString.indexOf("@") != 1){
//email is correct
processForm();
}else{
alert("That isn't a valid email adress.");
}

If you take the script that njs just posted (thank-you) and make it a function, you can then call it from an onSubmit command in the FORM tag.

So should it be like this?


function validation() {
if(email.value.indexOf("@") != 1){
        //email is correct
        processForm();
}else{
        alert("That isn't a valid email adress.");
}
}

Is processForm already a javascript function? I can’t test it because my server thing is very slow :-\ Oh yea i changed ‘youremailString’ to ‘email.value.indexOf’ where email is the form name. Would that work? thanks

no… processForm was just a function which submitted the form.

Unfortunately I don’t really know JavaScript. I just pulled that out of Flash AS.

Also, you don’t need a server to test JavaScript. You can just open the pages in IE or Mozilla or whatever browser you use.

Oh ok, well i’ll try it. I have to upload it to a server because i use php for the mail form :-\ Thanks for the help though njs and abzoid

I think i have found a good email validation code. But its pretty confusin :-\ I’ll work on it though…thanks :slight_smile: