473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
6 2110
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TAM | last post by:
Hi, I have a simple JavaScript code that ensures that all the form fields are filled and there is also a function that checks if the email is a valid address. For some reason IE is giving...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
2
by: daniel.boorn | last post by:
Form validation using JavaScript has never been as easy and simple! We have developed a free generic form validation script that can validate any form with very little JavaScript required in form!...
14
by: JNariss | last post by:
Hello, I am fairly new to asp and jscript and am now in the process of learning some form validation. I am taking one step at a time by validating each field and testing it before moving onto...
2
by: this one | last post by:
I have the following code <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript"...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
6
by: D | last post by:
Hello all...I have an issue with one of my java script functions that I'm hoping someone can easily help with. I have a web based application that we use to create/sign up for overtime. When we...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.