[SOLVED] Validate Form Class

This is a discussion on "[SOLVED] Validate Form Class" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Validate Form Class are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 27th, 2007, 16:38
New Member
Join Date: Nov 2007
Location: Portugal
Age: 27
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Validate Form Class

Hello, I'm trying to develop a series of classes to validate my webforms, I'll try to explain the idea:

I what to create a HTML form whiteout any javascript event, for example:

<form id="form1" name="form1" method="post" action="">
<input type="button" name="button" id="button" value="Button" />
</form>


I call the class in the head section (or in external javascript file)

For example:

var alert=new alert_button('button');

The class alert_button would have a method called onclick

For example:

alert_button.onclick=function(){
alert('HELLO');
}


What I would what to do is when I call var alert=new alert_button('button'); all the methods and properties from alert_button class woud be passed to the object of the element form whit the ID informed (in this case the id button that is from a input element)! This would be very useful because if I what to add a new methods to that elements I would only have to add the method to the class!

The problem is that I cant do that passing of methods, I think that the problem is because the object is already instantiated after I try to pass the new methods! (Note that the object type in this case is a input type button, but it could be any type, for example it could be a anchor link).

Here is a example of my code:

Code: Select all
<head>
<title>Class Test</title>
<script type="text/javascript">

    
    function elemento(id){
        this.id=id;
    }
    
    elemento.prototype.onclick=function()
    {
        return 0;
    }
    
    
    
    function botao(id,men){
        
        elemento.call(this,id);

        var targ=document.getElementById(id);
        
        this.men=men;
        
        targ.call(targ,this);
    }
    
    botao.prototype=new elemento();
    botao.prototype.onclick=function()
    {
        alert(this.men);
    }
    
    
    
    function loade()
    {
        var teste=new botao('button',"HELLO");
        var teste=new botao('button2',"HELLO TOO");
    }

</script>
</head>

<body onload="loade()">
<form id="form1" name="form1" method="post" action="">
  <input type="button" name="button" id="button" value="Button" />
  <input type="button" name="button" id="button2" value="Button" />
</form>

</body>
</html>
Can any one help me make this work?

Thanks for the help!
Reply With Quote

  #2 (permalink)  
Old Nov 28th, 2007, 12:10
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Validate Form Class

SO you wish to add methods after you have instansiated the object? Or do you want to call all of the methods at the same time as you instansiate the object, I can't quite understand what you mean.

Actually, I think I see what you mean now you want to get the form element by id the add properties and methods to that?

If this is the case you could try something like

Code: Select all

function functionOne () 
{
               this.myname = 'Rakuli';
               this.myPetsName = 'Peter';
}

function onClickFunction()
{
           alert ('Hello' + this.myname);
}

// now this is the constructor function, pass the id to this

function createAFormElementWIthExtraStuff(id)
{
           var id = document.getElementById(id);
           id.functionOne = functionOne;
          id.onclick = onClickFunction;
          return id;
}

// The you can create the elements like

var teste = createAFormElementWIthExtraSuff('button');
Hope that helps -- and makes sense
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Nov 28th, 2007, 14:51
New Member
Join Date: Nov 2007
Location: Portugal
Age: 27
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Validate Form Class

Thank you Rakuli, it worked 100% , thats just what I was looking for... . Just to help any one that what to folow the same idea, in your example:

Code: Select all
var teste = createAFormElementWIthExtraSuff('button');
Must set the name to:

Code: Select all
var teste = createAFormElementWIthExtraStuff('button');
There was a "t" missing in Stuff!

Tks for the help,
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate a form only if certain conditions are met svennie12 JavaScript Forum 2 May 10th, 2008 14:59
[SOLVED] How to Validate a HTML Contact Form Xhmtl Web Page Design 2 Dec 9th, 2007 20:11
Div class failing to validate saracen Web Page Design 2 May 8th, 2007 14:02
validate form Monie Classic ASP 1 Sep 2nd, 2004 09:06
validate form? Nick JavaScript Forum 10 Aug 31st, 2004 07:47


All times are GMT. The time now is 10:19.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43