[英]Javascript field validation form
1.Okay so I want to make my Country and Sex field a required field. 1.Okay所以我想让我的国家和性别领域成为必修科目。 I tried putting the required in the html code but its still not working.
我尝试将所需的内容放在html代码中,但它仍然无效。 I don't know if there is a java script function to validate them.
我不知道是否有一个java脚本函数来验证它们。
<div class="form-group">
<label for="country"><span class="required">* </span> Country: </label>
<select name="country" id="selCountry" class="form-control selectpicker" required>
<option value=" " selected>Please select your country</option>
<option value="AF">Afghanistan</option>
</select>
</div>
2.I want my phone number to be numerical values only and it should contain 8 digit only. 2.我希望我的电话号码只是数字值,它只能包含8位数字。 The code I wrote will erase everything each time the user input a letter in the phone field , so basically it will accept only digits.But the field should accept 8 digit only or it will output an error to the user.
我写的代码会在用户每次在手机字段中输入一个字母时删除所有内容,所以基本上它只接受数字。但是该字段只能接受8位数字,否则会向用户输出错误。
function validatephone(phone) {
var maintainplus = '';
var numval = phone.value
if (numval.charAt(0) == '+') {
var maintainplus = '';
}
curphonevar = numval.replace(/[\\A-Za-z!"£$%^&\,*+_={};:'@#~,.Š\/<>?|`¬\]\[]/g, '');
phone.value = maintainplus + curphonevar;
var maintainplus = '';
phone.focus;
}
Can anyone show me the right way to doing this ? 有人能告诉我这样做的正确方法吗?
Here is my jsfiddle code that I tried so far. 这是我到目前为止尝试过的jsfiddle代码。
https://jsfiddle.net/2oong6n2/2/ https://jsfiddle.net/2oong6n2/2/
1) you have already selected your option using this. 1)您已经使用此选择了您的选项。 so required value will always return true.
因此所需的值将始终返回true。 whether you give country name or not .
你是否给出了国名。
<option value=" " selected>Please select your country</option>
try this <option value="" selected>Please select your country</option>
试试这个
<option value="" selected>Please select your country</option>
2) for radio button you have put required attribute in label . 2)对于单选按钮,您在标签中放置了必需的属性。 you have to do it in input element .
你必须在输入元素中做到这一点。
<input type="radio" name="gender" value="no" required/> Female
https://jsfiddle.net/2oong6n2/3/ https://jsfiddle.net/2oong6n2/3/
See this jsfiddle to allow only numbers to be entered into the text field http://jsfiddle.net/Lm2hS/ 看到这个jsfiddle只允许在文本字段中输入数字http://jsfiddle.net/Lm2hS/
Use maxlength attribute for the input field to limit the number of characters to 8 digits 使用输入字段的maxlength属性将字符数限制为8位数
<input type="text" maxlength="8" />
<input type="radio" name="gender" value="yes" checked="checked"/>Male <input type="radio" name="gender" value="no" /> Female
<select name="country" id="selCountry" class="form-control selectpicker" required> <option value=" " selected>Please select your country</option> <option value="AF" selected>Afghanistan</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.