Here is my code:
function validate() {
if (document.getElementById('check_value').value == 'this') {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
I try doing this:
function validate() {
if (document.getElementById('check_value').value == 'this','that','those') {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
...and it just accepts absolutely anything that's entered. Is this even possible to do?
Here is my code:
function validate() {
if (document.getElementById('check_value').value == 'this') {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
I try doing this:
function validate() {
if (document.getElementById('check_value').value == 'this','that','those') {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
...and it just accepts absolutely anything that's entered. Is this even possible to do?
Share Improve this question edited Jul 26, 2013 at 5:22 RandomPleb asked Jul 26, 2013 at 4:23 RandomPlebRandomPleb 4241 gold badge5 silver badges20 bronze badges 1- If your ment is relevant to the question you should edit your question and add it. – Keith Smiley Commented Jul 26, 2013 at 4:41
7 Answers
Reset to default 3This works too.
function validate() {
var acceptableValues = {
this: undefined,
that: undefined,
those: undefined
},
value = document.getElementById('check_value').value;
if ( acceptableValues.hasOwnProperty(value) ) {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
and this:
function validate() {
var value = document.getElementById('check_value').value;
switch (value) {
case: 'this':
case: 'that':
case: 'those':
$('#block').fadeIn('slow'); break;
default:
alert("Nope. Try again");
}
}
Like this
var val = document.getElementById('check_value').value;
if (val == 'this' || val =='that' || val =='those'){
//pass
}
If you want to accept any thing just remove the if condition or check the length
of the value
if (val.length >0){
//pass
}
You can use an array for the allowed values and use indexOf to check. Like this:
function validate() {
if (['this','that','those'].indexOf(document.getElementById('check_value').value) == -1) {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
var correctArr = ['this','that','those'];
function validate(val){
isCorrect = false
$.each(correctArr, function(index,element){
if(val.indexOf(element)>-1){
isCorrect = true;
}
}
)
return isCorrect;
}
You can try regex test
like,
var myregex=/this|that|those/i;
function validate() {
if(myregex.test(document.getElementById('check_value').value)){
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
Updated, if you want to validate only for this,that and those
Then try this regex
,
var myregex=/^(this|that|those)$/i;
The short answer is no.
You need to pare every single option with the ||
operator.
An other option would be to use regexp like that :
function validate() {
var values = ['this', 'that', 'those'];
if (document.getElementById('check_value').value.search(new RegExp('^('+values.join('|')+')$')) > -1) {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
Fiddle : http://jsfiddle/78PQ8/1/
You can aswell create your own prototype this type of action:
String.prototype.is = function(){
var arg = Array.prototype.slice.call(arguments);
return this.search(new RegExp('^('+arg.join('|')+')$')) > -1;
}
and call is like that:
function validate() {
if (document.getElementById('check_value').value.is('this', 'that', 'those')) {
$('#block').fadeIn('slow');
}
else {
alert("Nope. Try again");
}
}
If you want every value to be possible:
var val = document.getElementById('check_value').value;
if( val.length > 0 ) {
}
Which means the string just has to be at least 1 character.. More mon way is:
if( val != '' ) {
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744359542a4570401.html
评论列表(0条)