【发布时间】:2020-12-28 15:00:20
【问题描述】:
如何验证两个单选按钮,用户应该能够选择两个单选按钮(电子邮件或电话)中的任何一个,然后会出现文本框字段。如果用户选择任何一个单选按钮,我想使用 Javascript 验证字段(电话或电子邮件)。
这是我试过的!
function validation()
{
var em = document.getElementById("eid").value.indexOf("@");
var ph = document.getElementById("ph").value;
if(em==-1)
{
alert("E-mail ID is not valid");
return false;
}
else if(ph==""||ph==null)
{
alert("Enter The Phone No");
return false;
}
else if(isNaN(ph)||ph.length>10||ph.length<10)
{
alert("The mobile no. always has 10 digit numerical value");
return false;
}
else{
alert("Your Information is Submitted Successfully");
}
</script>
</tr>
</html>
<html>
<style>
#Phone:checked ~ .Phone
{
display:block;
}
#Email:checked ~ .Email
{
display:block;
}
</style>
<tr>
<td align="right">
<label>Email</label>
</td>
<td align="left">
<input type="radio" name="data" id="Email">
<div class="Email" hidden>
<input type="text" id="eid" placeholder="Enter your Email" />
</div>
</td>
<td align="right">
<label>Phone</label>
</td>
<td align="left">
<input type="radio" name="data" id="Phone">
<div class="Phone" hidden>
<input type="text" id="ph" minlength="1" maxlength="10" placeholder="Enter your phone number" />
</div>
</td>
<input type='submit' name='submit' onclick="validation();" />
</html>
【问题讨论】:
标签: javascript html