JavaScript实现发送短信验证码
以下是使用JavaScript实现发送短信验证码的完整源码:
HTML代码:
<form>
<label for="phone">手机号码:</label>
<input type="tel" id="phone" name="phone" required maxlength="11">
<button type="button" id="sendCodeButton" onclick="sendCode()">发送验证码</button>
<br>
<label for="code">验证码:</label>
<input type="text" id="code" name="code" required maxlength="6">
<button type="submit">提交</button>
</form>
JavaScript代码:
// 获取发送短信验证码的按钮
var sendCodeButton = document.getElementById("sendCodeButton");
// 获取手机号码输入框
var phoneInput = document.getElementById("phone");
// 发送短信验证码函数
function sendCode() {
// 获取手机号码
var phone = phoneInput.value;
// 模拟后端发送短信验证码
// 在实际项目中需要使用后端接口发送短信验证码
console.log("向手机号