SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Form Validation

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Validation

    I am working on creating my own field validation script and I need some help. Here is my code...
    Code:
    String.prototype.trim = function()
    {
    	a = this.replace(/^\s+/, '');
    	return a.replace(/\s+$/, '');
    };
    
    function SignUpCheckFields()
    {
    	var AcceptTerms = document.getElementById('AcceptTerms');
    	if (AcceptTerms.checked == false)
    	{
    		alert("You must accept terms to join!")
    		return false;
    	}
    	else
    	{
    		CheckFieldEmpty('FirstName', 'Error', 'Please fill out all of the required fields.');
    	}
    }
    
    function CheckFieldEmpty(Field, Error, ErrorMessage)
    {
    	var Field = document.getElementById(Field);
    	var Error = document.getElementById(Error);
    	if (Field.value.trim == "")
    	{
    		Error.innerHTML = ErrorMessage
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    If the user does not check Accept Terms it alerts saying, You must accept terms to join! If this is checked it is submitting the form but I want it to check if FirstName is empty or not. If FirstName is empty I want the Error Div to display the following text, Please fill out all of the required fields, and I don't want the form to submit. If FirstName isn't empty then I want the form to submit. Any help would be greatly appreciated.

    Thanks,
    HighCaliber
    Last edited by HighCaliber; Oct 15, 2006 at 17:39.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HighCaliber
    if (Field.value.trim == "")
    You may want to re-appraise that line...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •