I am trying form valdiation with javascript but it never call the function. can someone please test this code and see why it is like that. It screwed up my mind.
here is my code: - <!-- Java Script form form validation and entries -->
-
<script language="JavaScript">
-
function checkForm()
-
{
-
alert("I am called");
-
var cfirstname, cemail, cpass, clastname;
-
-
with(window.document.msgform)
-
{
-
cfirstname = firstname;
-
cemail = email;
-
cpass = pass;
-
clastname = lastname;
-
}
-
-
if(trim(cfirstname.value) == '')
-
{
-
alert('Please enter your first name');
-
cfirstname.focus();
-
return false;
-
}
-
else if(trim(cemail.value) == '')
-
{
-
alert('Please enter your email');
-
cemail.focus();
-
return false;
-
}
-
else if(!isEmail(trim(cemail.value))) // validate email address
-
{
-
alert('Email address is not valid');
-
cemail.focus();
-
return false;
-
}
-
else if(trim(cpass.value) == '')
-
{
-
alert('Please enter your password');
-
cpass.focus();
-
return false;
-
}
-
else if(trim(clastname.value) == '')
-
{
-
alert('Please enter your lastname');
-
clastname.focus();
-
return false;
-
}
-
else
-
{
-
cfirstname.value = trim(cfirstname.value);
-
cemail.value = trim(cemail.value);
-
cpass.value = trim(cpass.value);
-
clastname.value = trim(clastname.value);
-
return true;
-
}
-
}
-
-
function trim(str)
-
{
-
return str.replace(/^\s+|\s+$/g,'');
-
}
-
-
function isEmail(str)
-
{
-
var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|
-
ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|
-
bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|
-
ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|
-
dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|
-
gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|
-
hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|
-
kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|
-
ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|
-
mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|
-
nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|
-
re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|
-
su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|
-
ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|
-
zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
-
-
return regex.test(str);
-
}
-
</script>
-
-
-
<?php
-
// Connects to your Database
-
include_once ('connection.php');
-
include_once ('class.countries.inc.php');
-
//This code runs if the form has been submitted
-
if (isset($_POST['submit'])) {
-
echo("going to check");
-
echo "<SCRIPT LANGUAGE='javascript'>checkForm();</SCRIPT>";
-
//This makes sure they did not leave any fields blank
-
if (!$_POST['firstname'] |!$_POST['lastname'] |!$_POST['email'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['company'] | !$_POST['address1']| !$_POST['city']| !$_POST['province']
-
| !$_POST['country']| !$_POST['postalcode']| !$_POST['phone']| !$_POST['sub1']| !$_POST['sub2']
-
) {
-
die('You did not complete all of the required fields');
-
}
-
// checks if the username is in use
-
if (!get_magic_quotes_gpc()) {
-
$_POST['email'] = addslashes($_POST['email']);
-
}
-
$usercheck = $_POST['email'];
-
$check = mysql_query("SELECT email FROM member WHERE email = '$usercheck'")
-
or die(mysql_error());
-
$check2 = mysql_num_rows($check);
-
//check if the name exists it gives an error
-
if ($check2 != 0) {
-
die('Sorry, the email '.$_POST['email'].' is already in use.');
-
}
-
// makes sure both passwords entered match
-
if ($_POST['pass'] != $_POST['pass2']) {
-
die('Your passwords did not match. ');
-
}
-
// encrypt the password and add slashes if needed
-
$_POST['pass'] = md5($_POST['pass']);
-
// check for addslashes
-
if (!get_magic_quotes_gpc()) {
-
$_POST['firstname'] = addslashes($_POST['firstname']);
-
$_POST['lastname'] = addslashes($_POST['lastname']);
-
$_POST['pass'] = addslashes($_POST['pass']);
-
$_POST['email'] = addslashes($_POST['email']);
-
$_POST['company'] = addslashes($_POST['company']);
-
$_POST['address1'] = addslashes($_POST['address1']);
-
$_POST['city'] = addslashes($_POST['city']);
-
$_POST['province'] = addslashes($_POST['province']);
-
$_POST['country'] = addslashes($_POST['country']);
-
$_POST['postalcode'] = addslashes($_POST['postalcode']);
-
$_POST['phone'] = addslashes($_POST['phone']);
-
$_POST['sub1'] = addslashes($_POST['sub1']);
-
$_POST['sub2'] = addslashes($_POST['sub2']);
-
}
-
-
// insert it into the database
-
/* $insert = "INSERT INTO member (firstname,lastname,company,region,title,address1,address2,city,province,country,postalcode,
-
phone,fax,email,password,website,noofsubscriber,noofaddressablesubscriber)
-
VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['company']."', '".$_POST['region']."',
-
'".$_POST['title']."', '".$_POST['address1']."', '".$_POST['address2']."', '".$_POST['city']."', '".$_POST['province']."',
-
'".$_POST['country']."',' ".$_POST['postalcode']."', '".$_POST['phone']."', '".$_POST['fax']."', '".$_POST['email']."', '".$_POST['pass']."',
-
'".$_POST['website']."', '".$_POST['sub1']."', '".$_POST['sub2']."')";
-
$add_member = mysql_query($insert); */
-
$insert = "INSERT INTO member (firstname,lastname,company,region,title,
-
address1,address2,city,province,country,postalcode,
-
phone,ext,fax,email,password,website,noofsubscriber,noofaddressablesubscriber)
-
VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['company']."', '".$_POST['region']."', '".$_POST['title']."',
-
'".$_POST['address1']."', '".$_POST['address2']."', '".$_POST['city']."', '".$_POST['province']."', '".$_POST['country']."', '".$_POST['postalcode']."',
-
'".$_POST['phone']."', '".$_POST['ext']."', '".$_POST['fax']."', '".$_POST['email']."', '".$_POST['pass']."',
-
'".$_POST['website']."', '".$_POST['sub1']."', '".$_POST['sub2']."')";
-
$add_member = mysql_query($insert);
-
?>
-
<h1>Registered</h1>
-
<p>Thank you, you have registered - you may now login</a>.</p>
-
<?php
-
}
-
else
-
{
-
?>
-
<!-- action="<php echo $_SERVER['PHP_SELF']; ?>" -->
-
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="msgform" >
-
<table border="0">
-
<tr><td>First Name:</td><td>
-
<input type="text" name="firstname" maxlength="50" size=50>
-
</td></tr>
-
<tr><td>Last Name:</td><td>
-
<input type="text" name="lastname" maxlength="50" size=50>
-
</td></tr>
-
<tr><td>Company:</td><td>
-
<input type="text" name="company" maxlength="150" size=60>
-
</td></tr>
-
<tr><td>Divion/Region:</td><td>
-
<input type="text" name="region" maxlength="50" size=50>
-
</td></tr>
-
<tr><td>Title:</td><td>
-
<input type="text" name="title" maxlength="50" size=50>
-
</td></tr>
-
<tr><td>Address 1:</td><td>
-
<input type="text" name="address1" maxlength="150" size=60>
-
</td></tr>
-
<tr><td>Address 2:</td><td>
-
<input type="text" name="address2" maxlength="150" size=60>
-
</td></tr>
-
<tr><td>City:</td><td>
-
<input type="text" name="city" maxlength="50" size=50>
-
</td></tr>
-
<tr><td>State/Province:</td><td>
-
<input type="text" name="province" maxlength="50" size=50>
-
</td></tr>
-
<tr><td>Country:</td><td>
-
<!-- <input type="text" name="country" maxlength="50"> -->
-
<?php
-
$countryObj = new countries;
-
$countryselect = $countryObj->countrySelect(
-
array('name'=>'country',
-
'style'=>'width: 200px; color: blue;'
-
),$country?$country:"ca");
-
-
echo $countryselect;
-
?>
-
</td></tr>
-
<tr><td>Zip/Postal Code:</td><td>
-
<input type="text" name="postalcode" maxlength="6" size=50>
-
</td></tr>
-
<tr><td>Phone:</td><td>
-
<input type="text" name="phone" maxlength="15"> Ext: <input type="text" name="ext" maxlength="4" size=4>
-
</td></tr>
-
<tr><td>Fax:</td><td>
-
<input type="text" name="fax" maxlength="15" size=50>
-
</td></tr>
-
<tr><td>Email:</td><td>
-
<input type="text" name="email" maxlength="100" size=60>
-
</td></tr>
-
<tr><td>Password:</td><td>
-
<input type="password" name="pass" maxlength="10" size=12>
-
</td></tr>
-
<tr><td>Confirm Password:</td><td>
-
<input type="password" name="pass2" maxlength="10" size=12>
-
</td></tr>
-
<tr><td>Website:</td><td>
-
<input type="text" name="website" maxlength="100" size=60>
-
</td></tr>
-
<tr><td># of Subscribes:</td><td>
-
<input type="text" name="sub1" maxlength="5" size=4>
-
</td></tr>
-
<tr><td># of Addressable Subscribers:</td><td>
-
<input type="text" name="sub2" maxlength="5" size=4>
-
</td></tr>
-
<tr><th colspan=2><input type="submit" name="submit" value="Submit Registeration" ></th></tr> </table>
-
</form>
-
-
<?php
-
}
-
?>
-
-
-
7 2095 Atli 5,058
Recognized Expert Expert
Are you calling the JavaScript function to check the fields after they have been submitted? That won't work. At that point there will be no fields to check.
Try attaching the JavaScript check function to the forms onsubmit event, or the submit buttons onclick event.
Also, the huge regex variable in your JavaScript code contains line-breaks, which is not allowed.
Markus 6,050
Recognized Expert Expert
If the *Javascript* function isn't called, then this *isn't* a PHP problem.
pbmods 5,821
Recognized Expert Expert
If the *Javascript* function isn't called, then this *isn't* a PHP problem.
Agreed. Moving to the JavaScript forum....
Now then.
Try adding an onsubmit handler to your form.
I am lost in this combination. I tried everything thing onsubmit() for form, action for form and onlick for submit button. Nothing worked at all except time waste. I alwasy prefer javascript for form validation but now...I dropped the idea to use javascript for php form validation, and used php-form-validation functions for this purpose. It took me a while to write that but that worked . If some want to post how i did that I'll be happy to do that.
Thanks All
gits 5,390
Recognized Expert Moderator Expert
please show how you tried to call the 'checkForm'-method on submit ... the validation must return a boolean value and the line should be similar to the following: - <form name="my_form" onsubmit="return checkForm();"/>
kind regards
Hi Dude, this is a simple javascript validation. just try this code this may help you
[HTML]</html>
<head>
<script type="text/javascript">
function validate()
{
var chars = document.getElementById('field').value;
if (chars.length < 130)
{
alert("The news story content must be a minimum of 130 characters to properly display. Please enter more content and re-submit the form.");
return false;
}
else
{
return true;
}
}
</script>
</head>
<form action="demo.html" method="post" onSubmit="return validate()">
<textarea id="field">
</textarea>
<input type="submit" value="Submit Me">
</form>
</html>[/HTML]
Regards
Ramanan Kalirajan
acoder 16,027
Recognized Expert Moderator MVP
I am lost in this combination. I tried everything thing onsubmit() for form, action for form and onlick for submit button. Nothing worked at all except time waste. I alwasy prefer javascript for form validation but now...I dropped the idea to use javascript for php form validation, and used php-form-validation functions for this purpose.
That was a big mistake in the first place. You should never depend on JavaScript validation alone. It's only there as a convenience to avoid unnecessary trips to the server. You MUST have server-side validation at all times.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: VbUser25 |
last post by:
Hi
Please suggest i think i am doing something wrong. I am calling fucntion
test from another function where i am performing all the validations.I
want to validate the email id.
this is the main function where i perform all sort of mandatory
validation==>
if (document.f.repemail.value != ""){
if(test(document.f.repemail.value=false)) {
|
by: simina |
last post by:
Hi...
I have a form with 4 number fields: phone area, phone number, cell
area, cell number.
I did a function that checks the "number" issue for all 4 fields in
the same time (because the code is onBlur: "return ph(this);"). Now
the problem is that if the user hits Enter data still goes to the
database (of course, because I should have the...
|
by: Hosh |
last post by:
I have a form on a webpage and want to use JavaScript validation for the
form fields.
I have searched the web for form validation scripts and have come up with
scripts that only validate individual fields, such as an "Email Validation
Script" or a "Phone Validation Script".
Is it ok to put all these scripts on page as they are or should they...
|
by: Tracey |
last post by:
Sorry for the repeated post. I tried to update a record in database using SqlCommand.ExecuteNonQuery( ) method (I failed using SqlDataAdapter). I traced the above statement and found that it returned 1 rows afftected. Then I checked my database, the data was not updated at all
Can someone give me a clue ?
|
by: Joey |
last post by:
Hi There,
I have a page with a form and submit button with validation and on the
header of the page I have 2 drop down menus and a submit button, when I
select either button it invokes the validation, is it possible to have 2
form buttons on a page doing different things?
Joey
| |
by: phillip.s.powell |
last post by:
Now I have another SQL query for MySQL I can't figure out!! This is
overwhelming me completely and I also must have this figured out today
and I can't figure it out!!
UPDATE student_db.student
SET has_letter1 = (
SELECT i.letter1 FROM olddb.student i, student_db student s WHERE
s.unique_key = i.unique_key
)
|
by: julie.siebel |
last post by:
Hello all!
As embarrassing as it is to admit this, I've been designing db driven
websites using javascript and vbscript for about 6-7 years now, and I
am *horrible* at form validation.
To be honest I usually hire someone to do it for me, grab predone
scripts and kind of hack out the parts that I need, or just do very
minimal validation...
|
by: SpiderSwamy |
last post by:
Hi,
I know little bit about asp, I am facing a problem in Validating
the ASP Form..
Example:
Stud ID: 501242016
FirstName: Ajit
LastName: Kar
|
by: smitanaik |
last post by:
hi i want to do validation of datetime in javascripti.e is i want it in this format dd:mm:yyyy:hh:mm:ss
plz heklp me out its urgent
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |