Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2011, 01:09 AM   PM User | #1
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
Simple validation query

I have a simple validation script. The first line of the validation is:

function Validate()
{
if (document.form.name.value == '')
{

the id of my form is form, so my query is will this validation work, as it does not have a name but it has an id. Should I change the name (above) to id?
quartzy is offline   Reply With Quote
Old 03-06-2011, 01:26 AM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
Without a "name" for your form, that code won't work. I would use a name for your form that is identical to the form's id.

Also, in general, you should avoid using an "id" or "name" that also refers to an element or attribute in the DOM.

So, putting the two ideas together, try something like this:

In your HTML:

Code:
<form id="form1" name="form1" method="post" action="file.php">
   <p>Username: <input type="text" name="username" /></p>
</form>
In your JS:

Code:
if (document.form1.username.value == '')
__________________
Regards, R.J.

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

Help spread the word! Like my YouTube-to-Mp3 Web Conversion Software on Facebook !! :)
chump2877 is offline   Reply With Quote
Users who have thanked chump2877 for this post:
quartzy (03-06-2011)
Old 03-06-2011, 01:56 AM   PM User | #3
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
Thanks for your post, I did wonder about that small thing.
quartzy is offline   Reply With Quote
Old 03-06-2011, 09:03 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Form validation of the pattern if (document.form1.username.value == '') is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
Philip M is offline   Reply With Quote
Old 03-06-2011, 04:59 PM   PM User | #5
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
Sorry I do not fully understand you, are you saying that my code is not going to work? I am not a regular member of this forum, so would not know if the validation has been covered many times either. I could delve into regular expressions but it is just for a simple form, and so would work OK I feel. with fields of name email and message.
quartzy is offline   Reply With Quote
Old 03-06-2011, 05:04 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Read what I said. You code will work, but is virtually useless as if the user enters even a single space or a ? sign it will pass the validation.

You can use the search feature of this forum to find out more about how to validate form fields.

Quote:
Originally Posted by quartzy View Post
I am not a regular member of this forum
639 posts and not a regular user?

Last edited by Philip M; 03-06-2011 at 05:07 PM..
Philip M is offline   Reply With Quote
Old 03-06-2011, 07:31 PM   PM User | #7
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Hi quartzy,

I usually do something along these lines for my client side validation. Instead of alerting the errors, another option is to highlight the input labels in the form with a different colour, typically red.

Code:
 
            function validateForm(){
                var inp1 = document.getElementById('txtInp1').value;
                //... similarly for other inputs to be validated
                var errMsg = "The following inputs are invalid:\n\n"
                var errors = new Array();
                var isDataValid = true;
                //validate inp1
                if(/[^0-9]/.test(inp1)){
                    isDataValid = false;
                    errors.push('inp1');
                }
                //validate the other inputs
                //...
                //display any error messages
                if(!isDataValid){
                    for(i=0; i < errors.length; i++){
                        errMsg += errors[i] + "\n";
                    }
                    alert(errMsg);
                }
                return isDataValid;
            }

Last edited by bullant; 03-06-2011 at 07:34 PM..
bullant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:28 AM.


Advertisement
Log in to turn off these ads.