我已经在javascript上创建了一个测验,一旦用户回答4/6或更正确,他们就会被要求提供名字,姓氏,然后给出随机的4位数代码。我遇到问题的部分是仅创建FirstName和LastName字母,也不允许和清空提示。
任何人都知道我如何实现正则表达式?
任何人都可以提供帮助吗?
function getAnswers(){
var amountCorrect = 0;
for(var i = 1; i <= 10; i++) {
var radios = document.getElementsByName('q'+i);
for(var j = 0; j < radios.length; j++){
var radio = radios[j];
if(radio.value == "1" && radio.checked) {
amountCorrect++;
}
}
}
alert("Correct number of answers: " + amountCorrect + " / 6");
if (amountCorrect <= 3){
alert("You have not passed on this occasion. You will now be taken back to the homepage.");
window.history.go(-1); // Go back a step
}
else{
var firstname = prompt("Please enter your first name.");
var lastname = prompt("Please enter your last name.");
alert("Your login Code for the store is: " + firstname.substring(1, 0) + lastname.substring(1, 0) + (generateCode())); // Do the generateCode function
close();
}
}
答案 0 :(得分:1)
试试这个..
var alphaExp = /^[a-zA-Z]+ [a-zA-Z]+$/;
var firstname =prompt("Please enter your first name And Last Name","");
if (firstname ==null || firstname =="")
{
alert("First and last name must be filled out!");
location.reload(true);
}
else if (!firstname.matches(alphaExp))
{
alert("Name must contain letters only!")
location.reload(true);
}
else{ // your code here.
}
答案 1 :(得分:0)
试试这个正则表达式:
^[A-Z][a-zA-Z]*$
此正则表达式匹配一串字母:
答案 2 :(得分:-2)
/^[a-zA-Z]+$/
这适用于你。