473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Untitl ed Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!--
function chkFormular()
{
if(document.for mulario.nombre. value == "") {
alert("Introduc e tu nombre!");
document.formul ario.nombre.foc us();
return false;
}
if(document.for mulario.email.v alue == "") {
alert("Introduc e tu email!");
document.formul ario.email.focu s();
return false;
}
if(document.for mulario.email.v alue.indexOf('@ ') == -1) {
alert("a ver amigo te has dejado la @ en tu email!");
document.formul ario.email.focu s();
return false;
}
var arroba = "@."
if(document.for mulario.email.v alue.indexOf(ar roba) > -1) {
alert("tio, tu email no es correcto!");
document.formul ario.email.focu s();
return false;
}

if(document.for mulario.email.v alue.indexOf('. ') == -1) {
alert("a ver chaval te has dejado un punto en tu email!");
document.formul ario.email.focu s();
return false;
}
if(document.for mulario.email.v alue.length < 6){
alert("macho tu email aún no esta bien!");
document.formul ario.email.focu s();
return false;
}
}
//-->
</script>
</head>

<body bgcolor="#99999 9">
<form action="" method="post" enctype="text/plain" name="formulari o"
id="formulario " onSubmit="retur n 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"><la bel>
<input type="radio" name="valoracio n web" value="mala">
mala</label></td>
<td width="37" bgcolor="#00000 0"><div align="center"> <img
src="kaka.jpg" width="38" height="39"></div></td>
</tr>
<tr>
<td><label>
<input type="radio" name="valoracio n web" value="regular" >
regular</label></td>
<td bgcolor="#00000 0"><div align="center"> <img src="STAR.jpg"
width="26" height="26"></div></td>
</tr>
<tr>
<td><label>
<input type="radio" name="valoracio n web" value="buena">
buena</label></td>
<td bgcolor="#00000 0"><div align="center"> <img src="STAR.jpg"
width="26" height="26"><im g src="STAR.jpg" width="26"
height="26"></div></td>
</tr>
<tr>
<td><label>
<input type="radio" name="valoracio n web" value="muy buena">
muy buena</label></td>
--------------------------------------------------------------
Jul 23 '05 #1
6 2150
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.formul ario.email;
for(i=0; i< deniedChars.len gth; i++) {
if(item.value.i ndexOf(deniedCh ars[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.formul ario.email.valu e)) {
alert("Invalid E-mail Address has been found! Please re-enter.");
document.formul ario.email.focu s();
return false;
}

Jul 23 '05 #3
wrote on 29 dec 2004 in comp.lang.javas cript:
var deniedChars = new Array(",","!"," #","$");
function chkFormular()
{

var item = document.formul ario.email;
for(i=0; i< deniedChars.len gth; i++) {
if(item.value.i ndexOf(deniedCh ars[i]) != -1) {
alert("no va");
return false;
}
}
return true;
}


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

function chkFormularEven Better(){
var email = document.formul ario.email.valu e;
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.c om> 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**********@g mx.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="retur n 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(el ements['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.javas cript:
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.formul ario.myTextfiel d.value
if(myTextfield! ='')alert('myTe xtfield 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
2282
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 "Object Expected" error on the alert statement. The same code doesn't give an error in NN or Opera or Mozilla or FireFox. Can someone give me a clue as what could be wrong here?
7
3609
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> </head> <style type="text/css">
7
2356
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 WinForms interface and then need to port that to a web app. I am kinda stuck on a design issue and need some suggestions / direction. Basically I have a business layer that I want to use to process any dataentry logic (row focus changes, data...
2
2364
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! For example: A normal HTML form with out validation: <form action="foobar.htm" method="get"> <div>Name: <input type="text" name="customerName" value=""></div>
14
2144
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 the next one to be sure I am correct. I ran into a problem with my validation when I added an else if code to my code. Here is what I tried to do: Form (ITTermination) has a field (EmployeeName) which I would like to validate to check for no...
2
4885
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" src="..\include\gen_validatorv2.js" type="text/javascript"></script> </head>
27
4745
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 appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
6
4307
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 have holidays we require our employees to sign up in 4 hr increments for the times we post. I'm having trouble creating a time slot that ends @ 12am. 12pm and all other hours work fine for start/end times. however 12am causes problems. My actual...
0
5569
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.