我正在使用下面的代码,但在用户跳过必填字段时需要错误消息。有人可以帮忙吗?因此,如果用户忘记填写所需的输入字段"名字",则在字段上方显示错误消息"名字是必填字段"。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//1
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
//2
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Company")
//3
do {
companies = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
和
<form id="comment_form" action="form.php" method="post">
<div class="compulsoryfield">
<input class="inputfield-radio" type="radio" name="gender" value="Mr" required><label class="label-radio">Mr.</label>
<input class="inputfield-radio" type="radio" name="gender" value="Ms"><label class="label-radio">Ms.</label>
<span class="requiredmark-radio">*</span>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="firstname" placeholder="first name" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield1" type="lastname" placeholder="last name" required>
</div>
<input class="inputfield2" type="companyname" placeholder="company name (if applicable)">
<input class="inputfield2" type="customernumber" placeholder="customer number (on invoice if available)" >
<br><br>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="email" placeholder="email address" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield1" type="emailagain" placeholder="re-enter email address (to confirm)" required>
</div>
<input class="inputfield2" type="telephonenumber" placeholder="telephone number (country code included)">
<br><br>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="subject" placeholder="subject of message" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<textarea id="textareafieldid" class="textareafield" name="message" placeholder="add your message here" rows="8" cols="39" required></textarea></div><br><br>
<p id="recaptcha-header">before sending, please show us you're real:</p>
<div><span class="requiredmark">*</span><div id="g-recaptcha-outer" class="compulsoryfield2">
<div class="g-recaptcha" data-sitekey="mySitekey"></div></div><br><br>
<input id="button-type1" type="submit" name="submit" value="SEND">
</form>
答案 0 :(得分:0)
我建议你尝试js插件,如: http://parsleyjs.org/ 要么 http://www.formvalidator.net/
你会看到,第一个对我来说是最好的,第二个对我很简单。
使用秒,你只需要在你的领域添加属性规则,如:
数据验证=&#34;需&#34;
通过此示例,您可以需要一个字段。
答案 1 :(得分:0)
将表单的目标发送到表单所在的同一页面。 在将任何html代码发送到客户端之前的页面顶部,进行表单处理。
$problemstring = "";
$firstname = "";
if(isset($_POST['submit'])){
if(!isset($_POST['firstname'])){$problemstring = "Error in firstname"}
//repeat for all fields
}
if(strlen($problemString)==0) {
//Do database actions here
//The Line below redirects to another page if form good
if(strlen($problemString)==0) die(header('Location: index.php'));
}
此代码检查表单中是否有空白数据,如果运行数据库操作没有问题。如果出现问题,页面的其余部分将加载。 将代码放在您希望显示错误消息的位置下方。
<?php
if(strlen($problemString)>0) {
echo '<table><tr>';
echo '<td colspan="3">';
echo '<font color="#FF0000"><strong>There was a problem with your form</strong><br>';
echo $problemString;
echo '</font></td>';
echo '</tr>';
echo '<tr><td colspan="3" height="16"></td></tr></table>';
}
?>
在您的表单中执行以下操作:
<input class="inputfield3" type="firstname" placeholder="first name" value="<?php echo $firstname; ?>" required>
这将使用在提交之前输入到表单中的内容填充该字段,如果这是空字段则为空白。如果第一次加载页面,则该值将为空(或者您设置的任何内容)。
我已经在我的网站上使用过它,效果很好。