Welcome, Guest
Username: Password:

TOPIC: Validate text input from database

Validate text input from database 2 years 6 months ago #100551

  • wilfredor
  • wilfredor's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
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
The administrator has disabled public write access.

Validate text input from database 2 years 6 months ago #100553

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5780
  • Thank you received: 1258
  • Karma: 514
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.
The administrator has disabled public write access.

Validate text input from database 2 years 6 months ago #100555

  • wilfredor
  • wilfredor's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
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.
The administrator has disabled public write access.

Validate text input from database 2 years 6 months ago #100557

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5780
  • Thank you received: 1258
  • Karma: 514
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.
The administrator has disabled public write access.
The following user(s) said Thank You: wilfredor

Validate text input from database 1 year 1 month ago #118310

  • noob
  • noob's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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
Last Edit: 1 year 1 month ago by noob.
The administrator has disabled public write access.

Validate text input from database 1 year 1 month ago #118312

  • noob
  • noob's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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";
		}
?>
Last Edit: 1 year 1 month ago by noob.
The administrator has disabled public write access.

Validate text input from database 1 year 1 month ago #118313

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1673
  • Thank you received: 413
  • Karma: 106
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
All LS releases => bit.ly/1VMuTDu | 2.06lts => bit.ly/1Qv44A1
Demo surveys => bit.ly/20NW9V8 (already included in /docs/demosurveys)
The administrator has disabled public write access.
The following user(s) said Thank You: noob

Validate text input from database 1 year 1 month ago #118315

  • noob
  • noob's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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!
The administrator has disabled public write access.

Validate text input from database 1 year 1 month ago #118318

  • noob
  • noob's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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>
Last Edit: 1 year 1 month ago by noob.
The administrator has disabled public write access.

Validate text input from database 1 year 1 month ago #118319

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5780
  • Thank you received: 1258
  • Karma: 514
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.
The administrator has disabled public write access.

Validate text input from database 1 year 1 month ago #118320

  • noob
  • noob's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.193 seconds