By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.

help me! I give up with the javascript I wrote to validate a simple form :-(

P: n/a
ok , first of all sorry if my english is not so good, I do my best.

here is my problem:

I don“t know much javascript so I wrote a very simple one to validate
a form I have on my webpage.

could you please have a look at the following script:

------------------------------------------------------------
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!--
function chkFormular()
{
if(document.formulario.nombre.value == "") {
alert("Introduce tu nombre!");
document.formulario.nombre.focus();
return false;
}
if(document.formulario.email.value == "") {
alert("Introduce tu email!");
document.formulario.email.focus();
return false;
}
if(document.formulario.email.value.indexOf('@') == -1) {
alert("a ver amigo te has dejado la @ en tu email!");
document.formulario.email.focus();
return false;
}
var arroba = "@."
if(document.formulario.email.value.indexOf(arroba) > -1) {
alert("tio, tu email no es correcto!");
document.formulario.email.focus();
return false;
}

if(document.formulario.email.value.indexOf('.') == -1) {
alert("a ver chaval te has dejado un punto en tu email!");
document.formulario.email.focus();
return false;
}
if(document.formulario.email.value.length < 6){
alert("macho tu email aśn no esta bien!");
document.formulario.email.focus();
return false;
}
}
//-->
</script>
</head>

<body bgcolor="#999999">
<form action="" method="post" enctype="text/plain" name="formulario"
id="formulario" onSubmit="return chkFormular()">

--------------------------------------------------------

don“t laugh at me :-) ,
I know it must be the most stupid javascript you have ever seen, but
it works, the thing is, that I want also check at the email textfield,
if the user has introduced characters like:( $ % / ?) etc..., all
characters which are never part of a valid email. I am still to stupid
to wrote some javascript lines to solve this problem, I tried all day
and finally I gave up, with a big headage.
could you help me out please??? I hope there is a simple trick to
solve my problem. In the same way is there any simple trick to control
if the user had introduced a insult in a textfield?

hope someone can help me ,

regards

Francisco Lopez with a big headage :-)


the next problem I have is not so important but also a one I have a
headage, how to know if someone checked a radiobutton on my form.

here you can see the part of the form where I have the radiobuttons:

----------------------------------------------
<td width="151"><label>
<input type="radio" name="valoracion web" value="mala">
mala</label></td>
<td width="37" bgcolor="#000000"><div align="center"><img
src="kaka.jpg" width="38" height="39"></div></td>
</tr>
<tr>
<td><label>
<input type="radio" name="valoracion web" value="regular">
regular</label></td>
<td bgcolor="#000000"><div align="center"><img src="STAR.jpg"
width="26" height="26"></div></td>
</tr>
<tr>
<td><label>
<input type="radio" name="valoracion web" value="buena">
buena</label></td>
<td bgcolor="#000000"><div align="center"><img src="STAR.jpg"
width="26" height="26"><img src="STAR.jpg" width="26"
height="26"></div></td>
</tr>
<tr>
<td><label>
<input type="radio" name="valoracion web" value="muy buena">
muy buena</label></td>
--------------------------------------------------------------
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hey Francisco,
Here is a little snippet of some code to help you.
Basically add any unwanted chars to the deniedChars array and loop
through them.
enjoy...

var deniedChars = new Array(",","!","#","$");
function chkFormular()
{

var item = document.formulario.email;
for(i=0; i< deniedChars.length; i++) {
if(item.value.indexOf(deniedChars[i]) != -1) {
alert("no va");
return false;
}
}
return true;
}

Jul 23 '05 #2

P: n/a
Throw this function into your script:

function checkEmail(str) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
return true;
}
return false;
}

Then inside your existing validation function, add this conditional
check:

if(!checkEmail(document.formulario.email.value)) {
alert("Invalid E-mail Address has been found! Please re-enter.");
document.formulario.email.focus();
return false;
}

Jul 23 '05 #3

P: n/a
wrote on 29 dec 2004 in comp.lang.javascript:
var deniedChars = new Array(",","!","#","$");
function chkFormular()
{

var item = document.formulario.email;
for(i=0; i< deniedChars.length; i++) {
if(item.value.indexOf(deniedChars[i]) != -1) {
alert("no va");
return false;
}
}
return true;
}


function chkFormular(){
var email = document.formulario.email.value;
if (/[!#$]/.test(email))
{alert("no va");return false;}
return true;
}

function chkFormularEvenBetter(){
var email = document.formulario.email.value;
if (/[!#$]/.test(email) || !/@/.test(email))
{alert("no va");return false;}
return true;
}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #4

P: n/a
On 28 Dec 2004 15:31:30 -0800, Spats30 <ho****@yahoo.com> wrote:
Throw this function into your script:

function checkEmail(str) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
That disallows perfectly valid addresses from all of the following
top-level domains (TLDs): aero, coop, info, museum, and name.
return true;
}
return false;
}


Why not

return [reg-exp].test(str);

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

P: n/a
On 28 Dec 2004 13:56:52 -0800, francisco lopez <bu**********@gmx.net>
wrote:
ok , first of all sorry if my english is not so good, I do my best.
Your English (captial E :P ) is fine.

[snip]
<script type="text/javascript">
<!--
You don't need that comment delimiter any more. Leave it, and the closing
one later, out of your mark-up.
function chkFormular()
When validating a form, it's usually easier to pass a reference to the
validating function:

function myFunction(form) {
/* ... */
}

<form ... onsubmit="return myFunction(this);">

You can then access form controls with:

function myFunction(form) {var elements = form.elements;
/* elements['nombre'].value */
}

If you're going to access form control regularly, like you do with
'email', you could save a reference to that, too:

function myFunction(form) {
var elements = form.elements,
email = elements['email'];

/* email.value */
}

Notice that I haven't actually accessed the form using its name or id
attribute; you don't need to with this approach.

[snip]
[...] I want also check at the email textfield, if the user has
introduced characters like:( $ % / ?) etc..., all characters which are
never part of a valid email.
As I read the address specification, those four characters you mention
*are* valid. Of course, that doesn't necessarily mean that a particular
mail system was written to cope with them (though omitting that
functionality would make it a broken implementation).

Attempting to accurately validate an e-mail address is futile at best; the
complete allowable syntax is pretty complicated. Instead, just check that
it's sane and if you really need to be sure, send a confirmation message
to the user.

/^[^\w@]+@[^\w.@]+(\.[^\w.@]+)+$/

and even that's restrictive to a certain extent. You'd use that regular
expression something like:

if(!/^[^\w@]+@[^\w.@]+(\.[^\w.@]+)+$/.test( str )) {
/* Address failed sanity check. */
}

where str is the string you want to test.

[snip]
In the same way is there any simple trick to control if the user had
introduced a insult in a textfield?
An insult? As in rude words? No. For a start, there are many words that
could be considered rude in a particular language which would make any
code bloated. There's also the fact that you might not only encounter rude
words from your own language. As I said, it's best to send a message to
any supplied address to confirm validity.

[snip]
[...] how to know if someone checked a radiobutton on my form.


You have to retrieve a collection (like an array, but with none of the
array methods) that contains all of the radio buttons. You then loop
through all of the elements and check if one is selected:

function isChecked(group) {
for(var i = 0, n = group.length; i < n; ++i) {
if(group[i].checked) {return true;}
}
return false;
}

With the code you presented, you'd call that function with:

if(isChecked(elements['valoracion web'])) {
/* A control is selected */
}

assuming it was with the validation code I presented above. If not, you'd
have to expand 'elements' to

document.forms['form-id-or-name'].elements

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

P: n/a
francisco lopez wrote on 28 dec 2004 in comp.lang.javascript:
In the same way is there any simple trick to control
if the user had introduced a insult in a textfield?


Do you mean "to check if the user has inserted some text"?

var myTextfield = document.formulario.myTextfield.value
if(myTextfield!='')alert('myTextfield is not empty.')

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.