如何在JS中调用两个函数 [英] how to call two function in JS
问题描述
我只是想让ot验证该字段,并在它得到公司,noofemps并通过电子邮件发送pdf时将数据发送到main.php文件.
问题是我可以进行字段验证并调用ajaxfuncation不起作用,我只需要一种使它们起作用的方法...
如果验证为true,则应调用ajaxfucation ..将数据发送到main.php ..
< form name ="form1">
......
.....
...
<输入类型=按钮"名称=保存"值=保存" onclick ="ajaxFunction();" />
</form>
Hi,
i just want ot validate the field and also send data to main.php file when it get company,noofemps and email a generate a pdf ...
the problem is that i can field validation and call ajaxfuncation are not working i just need a way to make them work ...
if validation are true then it should call ajaxfucation..to send data to main.php..
<form name="form1">
......
.....
...
<input type=''button'' name=''save'' value=''Save'' onclick="ajaxFunction();" />
</form>
<pre lang="cs">function formValidator(){
// Make quick references to our fields
var Company = document.getElementById(''Company'');
var Designation = document.getElementById(''Designation'');
var FirstName = document.getElementById(''First Name'');
var LastName = document.getElementById(''Last Name'');
var Phone = document.getElementById(''Phone'');
var Email = document.getElementById(''Email'');
var Website = document.getElementById(''Website'');
var Industry = document.getElementById(''Industry'');
var NoofEmployees = document.getElementById(''No of Employees'');
// Check each input in the order that it appears in the form!
if(isAlphanumeric(Company, "Please enter company name") || notEmpty(Company,"Please enter company name"){
if(isAlphabet(Designation, "Please enter title")){
if(isAlphabet(FirstName, "Please enter first name")){
if(isAlphabet(LastName, "Please enter last name")){
if(isAlphanumeric(Phone, "Please enter phone number")){
if(emailValidator(Email,"Please enter a valid email address")){
if(isValidURL(Website, "Please enter a valid URL address")){
if(madeSelection(Industry, "Industry is not selected")){
if(isNumeric(NoofEmployees, "Please enter number of employees")){
return true;
}
}
}
}
}
}
}
}
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isValidURL(url,helperMsg){
var urlExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
if(elem.value.match(urlExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
function madeSelection(elem, helperMsg){
if(elem.value == "-None-"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}
function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//document.myForm.time.value = ajaxRequest.responseText;
document.myForm.submit();
}
}
var Company = document.getElementById(''Company'').value;
var Email = document.getElementById(''Email'').value;
var NoofEmployees = document.getElementById(''NoofEmployees'').value;
var queryString = "?Company=" + Company + "&Email=" + Email + "&NoofEmployees=" + NoofEmployees ;
ajaxRequest.open("GET", "main.php" + queryString, true);
ajaxRequest.send(null);
}
推荐答案
/; if(elem.value.match(numericExpression)){ 返回true; }别的{ 警报(helperMsg); elem.focus(); 返回false; } } 函数isValidURL(url,helperMsg){ var urlExp =/^(([[\ w] +:)?\/\/)?(([\ d \ w] |%[a-fA-f \ d] {2,2})+(:( [\ d \ w] |%[a-fA-f \ d] {2,2})+)?@)?([\ d \ w] [-\ d \ w] {0,253} [\ d \ w] \.)+ [\ w] {2,4}(:[\ d] +)?(\/([-+ _〜.\ d \ w] |%[a-fA-f \ d] {2,2})*)*(\?(&?([-+ _〜.\ d \ w] |%[a-fA-f \ d] {2,2})=?)*) ?(#([-+ _〜.\ d \ w] |%[a-fA-f \ d] {2,2})*)?
/; if(elem.value.match(numericExpression)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } function isValidURL(url,helperMsg){ var urlExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?
/; if(elem.value.match(urlExp)){ 返回true; }别的{ 警报(helperMsg); elem.focus(); 返回false; } } 函数isAlphabet(elem,helperMsg){ var alphaExp =/^ [a-zA-Z] +
/; if(elem.value.match(urlExp)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } function isAlphabet(elem, helperMsg){ var alphaExp = /^[a-zA-Z]+
/; if(elem.value.match(alphaExp)){ 返回true; }别的{ 警报(helperMsg); elem.focus(); 返回false; } } 函数isAlphanumeric(elem,helperMsg){ var alphaExp =/^ [0-9a-zA-Z] +
/; if(elem.value.match(alphaExp)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } function isAlphanumeric(elem, helperMsg){ var alphaExp = /^[0-9a-zA-Z]+
这篇关于如何在JS中调用两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!