Validate text input from database

Mehr
4 Jahre 2 Monate her #100551 von wilfredor
wilfredor erstellte das Thema Validate text input from database
Greetings to all members of this forum.

This is my first post in this forum, I'm new in LimeSurvey development area, however, I possess knowledge of Jquery,JavaScript and php.

My question is focused on the validation of a simple text field. Validation would be as follows:

1. The user enters an ID in a question of simple text field.
2. The ID entered by the user is looked up in a table in a database-not limesurvey
3. In case the ID is found in the table database, the field is successfully validated

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
4 Jahre 2 Monate her #100553 von tpartner
tpartner antwortete auf das Thema: Validate text input from database
I think you'll need to fire an AJAX call to a remote script that accesses your ID. database and returns true/false. (probably on keyup or change of the text input)

Depending on the returned result, you could toggle a hidden question and then use conditions, relevance or quotas to control further access to the survey.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
4 Jahre 2 Monate her #100555 von wilfredor
wilfredor antwortete auf das Thema: Validate text input from database
I fully understand what needs to be done, however. My specific question is the way to do this is good practice. I mean, what files to edit.

Thank you very much.

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
4 Jahre 2 Monate her #100557 von tpartner
tpartner antwortete auf das Thema: Validate text input from database
You can place the code for the AJAX call in the question source or in template.js.

manual.limesurvey.org/Workarounds:_Manip....29_in_LimeSurvey.3F

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Folgende Benutzer bedankten sich: wilfredor

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 9 Monate her - 2 Jahre 9 Monate her #118310 von noob
noob antwortete auf das Thema: Validate text input from database
i followed your recomandation and was able to write a few lines:
<script type="text/javascript" charset="utf-8">
function checkID(str) {
    if (str.length == 0) { 
        document.getElementById("check").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("check").innerHTML = xmlhttp.responseText;
				document.getElementById("check1").value = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "id.php?q=" + str, true);
        xmlhttp.send();
    }
}
 
</script>
 
<p><b>Start typing a name in the input field below:</b></p>
 
First name: <input type="text" onkeyup="checkID(this.value)">
<p>Check: <span id="check"></span></p>
<br>
<input type="hidden" id="check1" value=""/>


ok now i need to know how to toggle a hidden object that was created by limesurvey and not with javascript and how to check this value like if!=0 with conditions

please help me somebody its urgent and im not very experienced in coding

my id.php file returns 0 and 1
Letzte Änderung: 2 Jahre 9 Monate her von noob.

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 8 Monate her - 2 Jahre 8 Monate her #118312 von noob
noob antwortete auf das Thema: Validate text input from database
now it looks like this, i think this way is better.
this time the code should be on the first page and the only question. i want to avoid users getting behind this section without a correct answer... the first part checks user input with id.php file. existing id returens 1, else 0. now i need to add the subit event but this doesnt work. and it is necessary to store the existing ID input in thelimesurvey database. the whole prucedure has to work for every login.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {	
	$("#1").keyup(function() {
        var txt = $("#1").val();
        $.post("id.php", {id: txt}, function(result) {
            $("#2").html(result);
			$("#3").val(result);
        });		
    });
	$("form#11").submit(function() {
			if ($("#3 input.text").val() == ""  || $("#3 input.text").val() == 0) {
				alert("false");
				return false;
			} else {
				return true;
			}
		});
	});
});	
</script>
</head>
<body>
<form id="11">
 
<input type="text" id="1">
<p>c: <span id="2"></span></p>
<input type="text" id="3" value=""/>
 
<button type="submit" id="movenextbtn" value="movenext" name="movenext" accesskey="n" class="submit button">Weiter</button>
</body>
</html>

i would appreciate your help!
thanks

id.php
<?php
// ID
$a[] = "1111";
$a[] = "2222";
$a[] = "3333";
 
$q = $_REQUEST["id"];
$match = 0; 
    foreach($a as $name) {
        if ($q !== "") {
			if ($q === $name) {
               $match = 1;
			   break;
            } else {
            $match = 0;
            }		
		}	
    }
	if ($match == 0){
		echo "0";
		}else{
		echo "1";
		}
?>
Letzte Änderung: 2 Jahre 8 Monate her von noob.

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 8 Monate her #118313 von Ben_V
Ben_V antwortete auf das Thema: Validate text input from database
It could be intersant for you to have a quick look at this recent post

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Folgende Benutzer bedankten sich: noob

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 8 Monate her #118315 von noob
noob antwortete auf das Thema: Validate text input from database
hey ben thanks for your reply
it helps me improoving my php file. but for now this is not my hardest trouble. the "autocompletition" works so far. but the second part, connecting the return of matching with the submit button of the form causes trouble in fact the script doesnt work after inserting
$("form#11").submit(function() {
			if ($("#3 input.text").val() == ""  || $("#3 input.text").val() == 0) {
				alert("false");
				return false;
			} else {
				return true;
			}
		});
	});
i think i do not understand the principle bihind the different { } sections. i tried to figure it out with alert commands but without success.any idea whats wrong with this part? the submit button has to be the last one from limesurvey to allow/deny survey progress... thanks a lot!

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 8 Monate her - 2 Jahre 8 Monate her #118318 von noob
noob antwortete auf das Thema: Validate text input from database
ok i made it

this works without limesurvey.
i store true / false return from my php file in a hidden input and check the submit button.
but in limesurvey it does not work...
in source of question with id 5 i paste <script> .. </script> and the input with id check_01 without whitespace between tags
the script doesnt run at all usually the 0 of var x appears in input id=check_01. it shows me if i did any sysntax mistakes and thats not the case without limesurvey...
anay idea why?
thanks
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
		var x = 0;
 
	$("#check_01").val(x);	
 
	$("#question5").keyup(function() {
  		   var txt = $("#question5").val();
			$.post("id.php", {id: txt}, function(result) {
			$("#check_01").val(result);
		});	
    });
 
	$("form#limesurvey").submit(function() {
		if( $("#check_01").val() == 0 || $("#check_01").val() == "" ) {
			alert("void");
			return false;
			} else {
			alert("true");
			return true;
		}
	});
});
</script>
</head>
<body>
<form id="limesurvey" action="test2.html" method="post">
<input type="text" id="check_01" value=""/>
<input type="text" id="question5" value=""/>
<button type="submit" id="movenextbtn" >Weiter</button>
</form>
</body>
</html>
Letzte Änderung: 2 Jahre 8 Monate her von noob.

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 8 Monate her #118319 von tpartner
tpartner antwortete auf das Thema: Validate text input from database
I don't understand why you are including jQuery and creating a new form. The LimeSurvey screen/group is already a form and jQuery is also already included. Surely you can use LimeSurvey elements (text questions) as your inputs.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Bitte Anmelden um an der Konversation teilzunehmen.

Mehr
2 Jahre 8 Monate her #118320 von noob
noob antwortete auf das Thema: Validate text input from database
hey! first i found the error in {id: txt} now its { id: txt }
the script is like this everthing else was just to understand how it works sorry for confusion.
my question is like this ID:5
Textarea: "Innput your ID"
pressing the source button and insert
<input type="hidden" id="check_01" value=""/>
<script>
$(document).ready(function() {
		var x = 0;
 
	$("#check_01").val(x);	
 
	$("#question5").keyup(function() {
  		   var txt = $("#question5").val();
			$.post("id.php", { id: txt }, function(result) {
			$("#check_01").val(result);
		});	
    });
 
	$("form#limesurvey").submit(function() {
		if( $("#check_01").val() == 0 || $("#check_01").val() == "" ) {
			alert("void");
			return false;
			} else {
			alert("true");
			return true;
		}
	});
});
</script>

i hope to accomplish this:
1: force a input of existing ID (id.php checks true false and send it back than i could display true/false)
2: input in question5 has to be stored in database of survey
3: submit button of form#limesurvey returns true if id.php returns true and my idea was to do this by checking a hidden input (id=check_01)

im totally inexperienced and this is my first try and me best idea to do it.

Bitte Anmelden um an der Konversation teilzunehmen.

Jetzt loslegen!

Melden Sie sich jetzt an, und erstellen Sie in wenigen Minuten Ihre erste Umfrage.

Account einrichten

Abonnieren Sie unseren Newsletter

Abonnieren Sie unseren Newsletter für alle Neuigkeiten rund um LimeSurvey
captcha