我想知道如何使用javascript验证输入字段并显示除文本框以外的每个字段的错误消息。我想开发一个包含姓名和联系电话的表格,但是每当输入正确或不正确的输入时,它就会显示我创建的错误消息。我不知道我做错了哪一部分。请帮帮我〜!
function validateForm()
{
var name = document.getElementById("custname").value
var regex1 = /^[a-zA-Z]+$/;
if(!name.match(regex1))
{
document.getElementById("nameError").innerHTML = "Name must be in the format of a-z or A-Z";
return false;
}
var contact = document.getElementById("contact").value
var regex2 = /^\(\d{2}\)\d{10}$/;
else if(!contact1.match(regex3))
{
document.getElementById("contactError").innerHTML = "Contact should be in format (CC)NNNNNNNNNN";
return false;
}
return true;
}
#nameError{
color:red;
}
#contactError{
color:red;
}
<html>
<form action="myCheck" method="get" onSubmit="return validateForm()">
<div>
<label for="name"><b>Customer Name: </b></label>
<div>
<input type="text" name="custname" id="custname" style="width:50%" placeholder="Only character a-z or A-Z">
<span id="nameError"></span>
<br /> <br />
</div>
</div>
<br />
<div>
<label for="name"><b>Contact Number: </b></label>
<div>
<input type="text" name="contact" id="contact" style="width:50%" placeholder="Only (CC)NNNNNNNNNN">
<span id="contactError"></span>
<br /> <br />
</div>
</div>
<br />
<input type="submit" value="Submit">
</form>
</html>
答案 0 :(得分:0)
您应该使用Regex match
方法而不是is not equal
运算符!=
。
有关更多信息,请参见此处:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/match
答案 1 :(得分:0)
要检查字符串是否与<?php
// Format POST values
foreach($_POST as $name => $value) {
$data .= $name.'='.$value.'&';
}
$data = substr_replace($data, NULL, -1);
$data = urlencode($data);
// THE PROBLEM COMES HERE, I TRY TO MAKE A CURL REQUEST TO PAYPAL BUT NO RESPONSE RETURNED
$ch = curl_init('https://sandbox.ipnpb.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
$res = curl_close($ch);
file_put_contents('result.txt',$res);
// There is nothing in my result.txt file for $res , but $data returns the POST data PayPal gave me
?>
匹配,应使用Regex
方法。
例如,对于您的案例,应使用:
.test()
答案 2 :(得分:0)
请使用此js代码
function validateForm(){
var name = document.getElementById("custname").value;
var contact = document.getElementById("contact").value
var letters = /^[A-Za-z]+$/;
if(!name.match(letters)&&isNaN(contact))
{document.getElementById("nameError").innerHTML = "Name must be in the format of a-z or A-Z";document.getElementById("contactError").innerHTML = "Contact should be in format (CC)";
return false;
}
else if(!name.match(letters))
{document.getElementById("nameError").innerHTML = "Name must be in the format of a-z or A-Z";document.getElementById("contactError").innerHTML = "Contact should be in format (CC)";
return false;
}
else if(isNaN(contact))
{
document.getElementById("contactError").innerHTML = "Contact should be in format (CC)";
return false;
}
return true;
}