Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:23:13+00:00 2026-06-17T16:23:13+00:00

I don’t know javascript well at all but I need to use it to

  • 0

I don’t know javascript well at all but I need to use it to validate inputs on forms i’ve made. I’ve looked at many examples and i really can’t see why it won’t work. When i click submit, nothing happens at all, it just goes to the next page, like there isnt even any javascript there. sorry if it’s a completely obvious answer, i have no experience with javascript so i have no idea what i’m looking for. i’m writing all of the code on the same page. thanks for any help.

javascript:

<script type="text/javascript">
function validateform(theform) {
var reason = "";

reason += validateName(theform.name);
reason += validateSurname(theform.surname);
reason += validateEmail(theform.email);
reason += validateHomephone(theform.homephone);
reason += validateMobilephone(theform.mobilephone);
reason += validateAddress(theform.address);
reason += validateTown(theform.town);
reason += validateCounty(theform.county);
reason += validatePostcode(theform.postcode);
reason += validateEmpty(theform.empty);


if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}

alert("All fields are filled correctly");
return true;
}




function validateEmpty(fld) {
var error = "";

if (fld.value.length == 0) {
    fld.style.background = 'Yellow'; 
    error = "The required field has not been filled in.\n"
} else {
    fld.style.background = 'White';
}
return error;  
}




function validateName(fld) {
var error = "";
var illegalChars = ^[a-z\s]*$; // allow letters, numbers

if (fld.value == "") {
    fld.style.background = 'Yellow'; 
    error = "You didn't enter a name.\n";
} else if (illegalChars.test(fld.value)) {
    fld.style.background = 'Yellow'; 
    error = "The name contains illegal characters.\n";
} else {
    fld.style.background = 'White';
}
return error;
}



function validatSurname(fld) {
var error = "";
var illegalChars = ^[a-z\s]*$; // allow letters, numbers

if (fld.value == "") {
    fld.style.background = 'Yellow'; 
    error = "You didn't enter a surname.\n";
} else if (illegalChars.test(fld.value)) {
    fld.style.background = 'Yellow'; 
    error = "The surname contains illegal characters.\n";
} else {
    fld.style.background = 'White';
}
return error;
}



function validateEmail(fld) {
var error="";
var tfld = trim(fld.value);                        // value of field with whitespace   trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
    fld.style.background = 'Yellow';
    error = "You didn't enter an email address.\n";
} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
    fld.style.background = 'Yellow';
    error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
    fld.style.background = 'Yellow';
    error = "The email address contains illegal characters.\n";
} else {
    fld.style.background = 'White';
}
return error;
}


function validateHomenumber(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

if (fld.value == "") {
    error = "You didn't enter a phone number.\n";
    fld.style.background = 'Yellow';
} else if (isNaN(parseInt(stripped))) {
    error = "The home phone number contains illegal characters.\n";
    fld.style.background = 'Yellow';
} else if (!(stripped.length == 11)) {
    error = "The home phone number is the wrong length.\n";
    fld.style.background = 'Yellow';
} else {
    fld.style.background = 'White';
}
return error;
}

function validateMobilenumber(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    


if (isNaN(parseInt(stripped))) {
    error = "The mobile phone number contains illegal characters.\n";
    fld.style.background = 'Yellow';
} else if (!(stripped.length == 11)) {
    error = "The mobile phone number is the wrong length.\n";
    fld.style.background = 'Yellow';
} else {
    fld.style.background = 'White';
}
return error;
}

function validateAddress(fld) {
var error = "";

if (fld.value == "") {
    error = "You didn't enter a home address.\n";
    fld.style.background = 'Yellow';

    }else {
    fld.style.background = 'White';
}
    return error;
}

function validateTown(fld) {
var error = "";

if (fld.value == "") {
    error = "You didn't enter a town or city.\n";
    fld.style.background = 'Yellow';

    }else {
    fld.style.background = 'White';
}
    return error;
}


function validateCounty(fld) {
var error = "";

if (fld.value == "") {
    error = "You didn't enter a county.\n";
    fld.style.background = 'Yellow';

    }else {
    fld.style.background = 'White';
}
    return error;
}

function validatePostcode(fld) {
var error = "";     
var illegalChars = ^[a-z\s]*$;
var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;

if (fld.value == "") {
    error = "You didn't enter a post code.\n";
    fld.style.background = 'Yellow';
     } 
     else if (illegalChars.test(fld.value)) {
    fld.style.background = 'Yellow'; 
    error = "The post code contains illegal characters.\n";
    else if ((fld.value.length < 7) || (fld.value.length > 8)) {
    error = "The post code is the wrong length. \n";
    fld.style.background = 'Yellow';
} else if (regpostcode.test(fld.value)) {
fld.style.background = 'Yellow';
error = "The post code isn't the correct format.\n";
}
} else {
    fld.style.background = 'White';
}
return error;
}


function trim(s)
{
return s.replace(/^\s+|\s+$/, '');
}

and the html

</script>
</head>
<body> 
<table class="formy" border="o" cellpadding="2" cellspacing="5" bgcolor="#F4EC53"  align="center">
<th colspan="2" align="center">Booking Private Hire</th>
<form name="firstform" method="post" onsubmit="return validateform(this)"  action="secondform.php">
<table summary="first form">
<tr>
<td><label for="Name">First name:</label></td>
<td><input name="Name" size="35" maxlength="50" type="text"></td>
</tr>   
<tr>
<td><label for="Surname">Surname:</label></td>
<td><input name="Surname" size="35" maxlength="25" type="password"></td>
</tr>   
<tr>
<td><label for="Email">E-mail address:</label></td>
<td><input name="Email" size="35" maxlength="30" type="text"></td>
</tr>  
<tr>
<td><label for="Homephone">Home phone number:</label></td>
<td><input name="Homephone" size="35" maxlength="25" type="text"></td>
</tr>  
<tr>
<td>
    <label for="Mobilephone">Mobile phone number:</label></td>
<td><input name="Mobilephone" size="35" maxlength="50" type="text"></td>
</tr>  
<tr>
<td>
    <label for="Address">Home address:</label></td>
<td><input name="Address" size="35" maxlength="50" type="text"></td>
</tr>  
<tr>
<td>
    <label for="Town">Town/city:</label></td>
<td><input name="Town" size="35" maxlength="50" type="text"></td>
</tr> 
<tr>
<td>
    <label for="County">County:</label></td>
<td><input name="County" size="35" maxlength="50" type="text"></td>
</tr> 
<tr>
<td>
    <label for="Postcode">Post code:</label></td>
<td><input name="Postcode" size="35" maxlength="50" type="text"></td>
</tr> 

<td> </td>
<td><input name="Submit" value="Send" type="submit" ></td>
<td> </td>

</table>
</form> 
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:23:14+00:00Added an answer on June 17, 2026 at 4:23 pm

    EDIT: The thing that finally got it working for me is this:
    The field names you are referencing do not match the actual names of the fields.

    reason += validateName(theform.name);
    

    changed to

    reason += validateName(theform.Name);
    

    Also, there appear to be errors in your javascript code:

    These lines do not parse:

    var illegalChars = ^ [a - z\s] * $; // allow letters, numbers
    

    validatePostcode() has a structural problem. This “else if” doesn’t have a closing bracket before it.

    else if ((fld.value.length < 7) || (fld.value.length > 8)) {
    

    You may have been going for:

        if (fld.value == "") {
            error = "You didn't enter a post code.\n";
            fld.style.background = 'Yellow';
        } else if (illegalChars.test(fld.value)) {
            fld.style.background = 'Yellow';
            error = "The post code contains illegal characters.\n";
        } else if ((fld.value.length < 7) || (fld.value.length > 8)) {
            error = "The post code is the wrong length. \n";
            fld.style.background = 'Yellow';
        } else if (regpostcode.test(fld.value)) {
            fld.style.background = 'Yellow';
            error = "The post code isn't the correct format.\n";
        } else {
            fld.style.background = 'White';
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't ask me why but i can't use this method because I need to
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't ask me why but I need to do the following: string ClassName =
I don't know why, but this code worked for me a month ago... maybe
Don't know what happened, but it just hangs on Type Space loading (4%) and
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't know if this is the right place to ask this, but I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.