i have problem in javascript email validation,
I wrote code something like,
//emp.php
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2" >
<table>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Desired Email-ID:</td>
<td><input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
</tr>
</table>
</form>
//java scrit code for checkMail()
<script language="javascript">
function checkMail() {
var email=document.getElementById('emp_email').value;
var mail = email.value;
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
if(!reg.test(mail) || mail == "")
{
alert("Your email address isn't valid!");
return false;
}
else {
alert("Email address is okay, let's send the form!");
}
}
</script>
Plz help me thanks in advance
The problem is that there is no element with id
emp_email.
Your element has name emp_email. Add an id attribute to the text box.
<input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/>
change it to
<input type="text" name="emp_email" id="emp_email" value="" size="32" onsubmit="checkMail()";/>
Edit:
To put javascript validation on submit button change
<input type="submit" value="Insert record" onclick="checkMail();"/>
to
<input type="submit" value="Insert record" onClick="return checkMail();" />
and remove the onsubmit event from the email textbox.
phoenix has the basic error right. But:
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
Don't put form validation on a submit onclick. It may be possible to submit the form without clicking the submit button (depending on the browser and number of fields and buttons in the form). Always put it on the form itself:
<form ... onsubmit="return checkMail();">
also:
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
That's a really bad idea, like most ad-hoc “e-mail validation” regexes it will reject many completely valid e-mail addresses. Your rules for what usernames and TLDs are allowed are particularly, pointlessly, restrictive.
See this infamous regex for how to actually validate e-mail addresses. Then cry, give up, and just do basic checks instead. (Is there an ‘#’ in it? is there a ‘.’ in it? are there no spaces? well fine then.)
(Even the page-long-horror-regex can't cope with non-ASCII e-email addresses via IDN, which it would be nice to support.)
Try moving the javascript function call off the submit button and onto an onblur event on the email field itself.
Related
I have placed my input type reset button inside form but its still not working.
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"><?php echo $rows["news"] ; ?></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /></td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /></td>
</tr>
</form>
I have also tried <button type="reset" value="Reset">Reset</button>
If you're expecting the reset button to empty out the form, that's not the way reset works. It will reset the form fields to what they were when the page was loaded. In this case, you have pre-filled content in your form, so clicking reset will simply undo any user changes since the page-load.
To reset all form fields to blank/nothing, you will need javascript. First get the form DOM object, then iterate over all the input types you want to reset, and set each field.value = '' (input text types / textarea), or modify attributes (for select, radio, checkbox etc.) to deselect and so on. Turn that into a function (there's probably a ready piece somewhere out there, I seem to have lost mine), and attach it to your reset button's click event -- or your form's reset event.
Edit: On a quick search, here's a basic example of how to clear a form to empty values.
Edit: If you're not concerned over anything but text fields, here's a simple way to do this. We add a "reset" event listener to the form, and when it fires, all fields are set to empty string.
function setFormCleanReset(formId) {
let formEl = document.querySelector(formId);
// Add event listener for reset event
formEl.addEventListener('reset', function(e) {
// Iterate all non-hidden fields, set values to ''
for(const fieldEl of formEl.querySelectorAll('input:not([type=hidden])')) {
// #todo check input type and handle "select" etc.
fieldEl.setAttribute('value', '');
}
});
}
// usage: setFormCleanReset('my_form_id');
I think you forgot to insert form tag in your html. It should be work if you insert button code into <form>..button code here..</form>.Something like this:
<form>
<input type="text">
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</form>
You should place the <input type="reset"> in a form like this,it will definitely work
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tring Reset</title>
<style>
</style>
</head>
<body>
<form>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type=reset></input>
</form>
</body>
</html>
You just need to put your reset button inside form. In following code I have taken one input box also an an example. I hope it helps.
<form>
<input type="text" />
<table>
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</table>
</form>
can you please try without echo anything inside textarea
try this
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /> </td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /> </td>
</tr>
</form>
I think reset button is working fine.
Excuse Me! You are using default value in input attribute. Remove default value from it. Then type anything then click rest it will make your text box empty.
I have a form submitting variables to another PHP file. In the second file, there is a button that leads back to the first file for eventual correction od submitted values. The values are retained in the form by $_POST. The "Reset" button works only when the form is used the first time (empty). After submitting and return from the second PHP file, the "Reset" doesn't do anything, no values are cleared. The button seems to be fully inactive.
<button title="<?php echo $lang['reset-button-hint']; ?>" alt="TOOLTIP 1" type="reset" name="reset"><?php echo $lang['reset-button']; ?></button>
#Jaspreet Kaur You need to add form tag outside the table. Please check my previous comment for it.
I am new to php. Though my doubt is very basic, but not understanding why my below form not able to submit? I am using actually image submit button.
Please tell me what wrong in below code:
<?php
function test(){
echo 'test';
}
var_dump($_POST['submit']); // here getting NULL, why?
if(isset($_POST['submit']))
{
test();
}
?>
<form action="." method=post name="loginform">
<table>
<tr>
<input type="password" style="display:none" />
<td width="130"><input type="password" name="password" size="15" maxlength="255" ></td>
<td width="136"><input type="image" src="button-log-in.gif" name="log in" alt=" log in" width="51" height="20" border="0"></td>
</tr>
</table>
</form>
There's no element which has 'submit' name. You're submitting two things, indicated by the name attributes: "password" and "log in". $_POST['...'] contains both of them.
If you'd like the function test() to be called upon submit, regardless of what is entered, you'd better add a hidden input field and check for its existence in PHP.
HTML:
...
<input type="hidden" name="some_name">
...
PHP:
if(isset($_POST['some_name']))
{
test();
}
is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario :
user browse to form.php
user gets an html form
user fills form and the form is sent back with POST method to form.php
form.php activate generalValidate($form) where form is the just recived form filled by user
generalValidate($form) returns true if this form is valid (for exemple properly filled), false else.
i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values?
dont eat me if its a bad question, im really just trying to learn :)
a code exemple to fill for your convinience :
<?php
function generalValidate($form) {
...
}
if (!isset($_SESSION))
session_start();
else if (generalValidate(...)) {
}
?>
<html>
<head>
</head>
<div>
<p>Register</p>
</div>
<form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
<table align="center">
<tr>
<td>First Name :</td>
<td><input name="fname" value="<?php if (isset($_POST["fname"])) {echo v($_POST["fname"]);}?>" required></input></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo v($_POST["lname"]);}?>" required></input></td>
</tr>
<tr>
<td>Email :</td>
<td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo v($_POST["email"]);} else {echo "xo#xo.xo";}?>" required></input></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
<tr>
<td>Retype password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
</table>
<input type="submit" value="Register" ></input>
</form>
</body>
</html>
Yes. Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way:
In your PHP script you could have something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Determines if the form was sent through the POST method
foreach ($_POST as $fieldName => $formValue) {
// Validate $formValue
}
}
What I think you want to ask is if form data can be manipulated without knowing the form's variable names. I say this because you want to have a general purpose function where the code can be reused for any form. Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form.
This code captures the form's input. All you have to do now is create a function that does whatever to the $name and $item values as they are looped through.
<?php
foreach($_POST as $name => $item) {
print "name::$name item::$item<br>";
}
?>
<html><title>test</title>
<form method="post">
field one: <input type="text" name="one">
<br>
field two: <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>
Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button).
Take a look at the answer here.
This is actually a canonical question for receiving form data in PHP. There are lots of ways to do it.
I want to submit a Form from home page to sub domain page.
Here is my code
html - home page (main domain)
<table>
<tr>
<td>Name</td>
<td><input type="text" name="txtName" id="txtName" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail" id="txtEmail" /></td>
</tr>
<tr>
<td><input name="btnSubmit" id="btnSubmit" value="Submit" type="button"></td>
</tr>
</table>
<form id="getDetails" method="post" action="http://customers.liyyas.com/">
<input type="hidden" name="act" value="Users" />
<input type="hidden" name="hdnName" id="hdnName" />
<input type="hidden" name="hdnEmail" id="hdnEmail" />
</form>
Script
<script type="text/javascript">
$(document).ready(function(){
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("getDetails").submit();
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
});
});
</script>
Sub domain page - user.php
<?php
$act = formatstring($_POST['act']);
switch($act)
{
case "Users":
$Name=$_POST['hdnName'];
$Email=$_POST['hdnEmail'];
print($Name);
exit();
}
?>
In sub domain I am printing the value but its not printing
Is it possible to submit a form from home domain to sub domain?
You need to change the action attribute of the form element from
http://customers.liyyas.com/
to
http://customers.liyyas.com/customers.php
I also assume you know that according to this code
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("getDetails").submit();
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
});
The form will submit BEFORE the values of hdnName and hdnEmail are changed? That may also be a bug for you to quickly switch solve by switching around a few lines. The reason this may be a bug is that when your form submits the page will be reloaded meaning the user will never get to see the new values inserted via JavaScript.
The fix could be
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
document.getElementById("getDetails").submit();
});
I want to perform an if statement based upon the value of a hidden input within myform.
the scenario is that I have a table consisting of tests. Each test is essentially its own form. the hidden input basically contains 'YES' if the test has a password attached. I want to prompt the user to enter the password if that is the case when they attempt to execute a particular test (submit the form).
so here is my code I am attempting:
$("#submit").live('click', function(event) {
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'YES') {
$('.messagepop').remove();
$(this).parent().append('<div class="messagepop pop"><p><label for="password">Enter test password</label><input type="text" size="30" name="pass" id="pass" /></p><p><input type="button" id="submitPass" value="GO" id="pass_submit"/> or <a class="close" href="/">Cancel</a></p></div>');
$(".pop").slideFadeToggle(function() {
$("#pass").focus();
});
return false;
}
});
Without the if statement the code works: on clicking a particular button within a form, another small password entry form pops up. However I only want the form to pop up if a value of a hidden input is 'YES'.
I have checked that values are present in the hidden input.
I imagine I am using the jQuery slightly wrong as I am a beginner. So can somebody identify a better way of performing the check?
Many thanks,
EDIT (jsfiddle with html etc):
jsfiddle
Your HTML is extremely malformed. The <tr> and <form> end-tags are mismatched, you've got a <form> tag directly inside the <tr>, which is not allowed, you haven't self-closed your <input /> tags and you have duplicate id's on the same page.
<tr>
<form name='myform' method='post' action='test_sim.php'>
<input name='test' type='hidden' value='1'>
<input type='hidden' name='has_password' value='NO'>
<td>TEST 1</td>
<td>B352</td>
<td>10</td>
<td>2011-05-12 06:00:00</td>
<td>2011-05-12 12:00:00</td>
<td>06:00:00</td>
<td><input id='submit' type='button' value='execute test'></td>
</tr>
</form>
It should be like this:
<tr>
<td>TEST 1</td>
<td>B352</td>
<td>10</td>
<td>2011-05-12 06:00:00</td>
<td>2011-05-12 12:00:00</td>
<td>06:00:00</td>
<td>
<form name='myform' method='post' action='test_sim.php'>
<input name='test' type='hidden' value='1' />
<input type='hidden' name='has_password' value='NO' />
<input class='submit' type='button' value='execute test' />
</form>
</td>
</tr>
Fixing the HTML solves your problem: http://jsfiddle.net/brianpeiris/ezFgU/16/
You should use the W3C Validator tool to check your HTML so that you can prevent these types of issues.
Edit: As mcgrailm mentioned, your id attributes are duplicated. jQuery is forgiving here, it seems, that's why the above jsFiddle works. You should use a class to identify your submit buttons and use the corresponding $('.submit') selector to attach a click handler.
i believe
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'YES')
should be
if ($(this).parent().find('input[name=has_password]:hidden').val() == 'YES')
i think your selector is incorrect try this
$("#submit").live('click', function(event) {
if ($(this).parent().find('input[name="has_password"]:hidden').val() == 'YES') {
$('.messagepop').remove();
$(this).parent().append('<div class="messagepop pop"><p><label for="password">Enter test password</label><input type="text" size="30" name="pass" id="pass" /></p><p><input type="button" id="submitPass" value="GO" id="pass_submit"/> or <a class="close" href="/">Cancel</a></p></div>');
$(".pop").slideFadeToggle(function() {
$("#pass").focus();
});
return false;
}
});
WORKING DEMO