Is Text Box data check possible?

Posted By: gamers

Is Text Box data check possible? - 02/13/20 14:49

Hello there,
How can I check if any data is entered into the textbox by users?
Also, how can I ensure that only numbers are entered in the text box?
Is it possible?
Posted By: 3run

Re: Is Text Box data check possible? - 02/13/20 15:25

Hey!

If you store text into the string, you can check it's length (str_len). As for 'numbers only' maybe str_stri can help, just a quick thought.

Greets!

Edit: I think str_stri was a bad idea, not going to work...probably str_cmpi is a better for this case.

Edit2: I've spent a little time and came up with this (since I don't know how to handle regex with lite-c and it might not even support it):
Code
#include <acknex.h>
#include <default.c>

#define PRAGMA_POINTER

STRING *non_numeric_str = "0123456789a";
STRING *numeric_str = "5325";
STRING *empty_str = "";

// receives a STRING and position (num) of a character in that STRING
// returns a single character as a STRING
STRING *get_next_char(STRING *src_str, int num)
{
	if(!src_str)
	{
		return NULL;
	}
	
	if(num > str_len(src_str))
	{
		return NULL;
	}
	
	STRING *temp_str = "";
	str_cpy(temp_str, src_str);
	str_trunc(temp_str, str_len(src_str) - num);
	str_clip(temp_str, num - 1);
	
	return temp_str;
}

// returns TRUE (1) if string is numeric
// otherwise returs FALSE (0)
var is_numeric_str(STRING *src_str)
{
	if(!src_str)
	{
		return false;
	}
	
	int i = 0, j = 0, res = true;
	
	for (i = 0; i < str_len(src_str); i++)
	{
		STRING *temp_str = get_next_char(src_str, i + 1);
		
		for (j = 0; j < 10; j++)
		{
			if (!str_cmpi(temp_str, str_for_num(NULL, j)))
			{
				res = false;
				continue;
			}
			
			res = true;
			break;
		}
		
		if(res == false)
		{
			break;
		}
	}
	
	return res;
}

// returns TRUE (1) is string's length is zero
// otherwise returns FALSE (0)
var is_empty_str(STRING *str)
{
	if(!str)
	{
		return false;
	}
	
	if(str_len(str) > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// main game function
void main()
{
	var numeric1 = is_numeric_str(non_numeric_str);
	var numeric2 = is_numeric_str(numeric_str);
	var empty = is_empty_str(empty_str);
	
	while(!key_esc)
	{
		DEBUG_VAR(numeric1, 0);
		DEBUG_VAR(numeric2, 20);
		DEBUG_VAR(empty, 40);
		
		wait(1);
	}
}


Edit3: my code above probably is not the best and easiest ways... I just wanted to give it a try. hopefully some other users may point out a better solution.
Posted By: gamers

Re: Is Text Box data check possible? - 02/14/20 12:50

Originally Posted by 3run
Hey!

If you store text into the string, you can check it's length (str_len). As for 'numbers only' maybe str_stri can help, just a quick thought.

Greets!

Edit: I think str_stri was a bad idea, not going to work...probably str_cmpi is a better for this case.

Edit2: I've spent a little time and came up with this (since I don't know how to handle regex with lite-c and it might not even support it):
Code
#include <acknex.h>
#include <default.c>

#define PRAGMA_POINTER

STRING *non_numeric_str = "0123456789a";
STRING *numeric_str = "5325";
STRING *empty_str = "";

// receives a STRING and position (num) of a character in that STRING
// returns a single character as a STRING
STRING *get_next_char(STRING *src_str, int num)
{
	if(!src_str)
	{
		return NULL;
	}
	
	if(num > str_len(src_str))
	{
		return NULL;
	}
	
	STRING *temp_str = "";
	str_cpy(temp_str, src_str);
	str_trunc(temp_str, str_len(src_str) - num);
	str_clip(temp_str, num - 1);
	
	return temp_str;
}

// returns TRUE (1) if string is numeric
// otherwise returs FALSE (0)
var is_numeric_str(STRING *src_str)
{
	if(!src_str)
	{
		return false;
	}
	
	int i = 0, j = 0, res = true;
	
	for (i = 0; i < str_len(src_str); i++)
	{
		STRING *temp_str = get_next_char(src_str, i + 1);
		
		for (j = 0; j < 10; j++)
		{
			if (!str_cmpi(temp_str, str_for_num(NULL, j)))
			{
				res = false;
				continue;
			}
			
			res = true;
			break;
		}
		
		if(res == false)
		{
			break;
		}
	}
	
	return res;
}

// returns TRUE (1) is string's length is zero
// otherwise returns FALSE (0)
var is_empty_str(STRING *str)
{
	if(!str)
	{
		return false;
	}
	
	if(str_len(str) > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// main game function
void main()
{
	var numeric1 = is_numeric_str(non_numeric_str);
	var numeric2 = is_numeric_str(numeric_str);
	var empty = is_empty_str(empty_str);
	
	while(!key_esc)
	{
		DEBUG_VAR(numeric1, 0);
		DEBUG_VAR(numeric2, 20);
		DEBUG_VAR(empty, 40);
		
		wait(1);
	}
}


Edit3: my code above probably is not the best and easiest ways... I just wanted to give it a try. hopefully some other users may point out a better solution.


Thanks for your contribution. I will try this as soon as possible.
Posted By: gamers

Re: Is Text Box data check possible? - 02/17/20 11:09

Is there any way to limit the value to be written in the text box between 0 and 100?
Posted By: 3run

Re: Is Text Box data check possible? - 02/17/20 14:05

I think it depends on how you created your text box. As far as I know, there is no default text box in acknex... if you use (f.e.) imgui there is already implemented text box with limitation etc.. You can check it out here.

Best regards!

Edit: maybe I misunderstood your question.. to input only 0...99 numbers, I would check for the length on the string (make sure it's numeric), if the length >= 3, then value is 100 and higher.
Posted By: gamers

Re: Is Text Box data check possible? - 02/18/20 05:29

Thank you for your answer 3run. Actually, I want to use only the textbox part of IMGUI. How can I do that? In other words, I want to create a text box that I want, to ensure that the data entered is only between 0 and 100, and to check whether the data is entered or not, and to warn the user if any data has not been entered into textbox.
Posted By: Emre

Re: Is Text Box data check possible? - 02/18/20 07:20

So, you want to input box for only numbers, between 0&100. Here is another way. Not the good one but at least it can give some idea;

Code
#include <acknex.h>


STRING* defstr="#100";
STRING* test_str="";


//is input box open
int is_input_box_on=0;

//test variable
int test_num;

//debug
function textx_startup()
{
	while(1)
	{
		if(is_input_box_on==1)
		{
			draw_text("Enter any number",1,1,COLOR_GREEN);
			draw_text(test_str,1,200,COLOR_GREEN);

			
		}
		DEBUG_VAR(test_num,20);
		wait(1);
	}
}

function on_f1_event() 
{
	beep();
	
	//clear all strings

	str_cpy(test_str,"");
	test_num=0;
	

	

	char this_char = 0;
	is_input_box_on=1;
	
	while(this_char!=13)//wait until press enter
	{
		

		this_char = inchar(defstr);
		if(this_char!=1)  
		{
			//if is numeric
			if(str_stri(this_char,"0")||
			str_stri(this_char,"1")||
			str_stri(this_char,"2")||
			str_stri(this_char,"3")||
			str_stri(this_char,"4")||
			str_stri(this_char,"5")||
			str_stri(this_char,"6")||
			str_stri(this_char,"7")||
			str_stri(this_char,"8")||
			str_stri(this_char,"9"))
			{
				str_cat(test_str,defstr);
				
			}
			else
			{
				//if isn't numeric don't do anything
			}
			
		}
		wait(1);
	}

	//convert string to number
	test_num=str_to_int(test_str);
	
	//turn of the box
	is_input_box_on=0;

	
	//if bigger than 100
	if(test_num>100)
	{
		printf("Error: Value is bigger than 100!");
		
		//try again
		on_f1_event(); 
	}

}




function main()
{

	on_f1_event();
}
void on_esc_event()
{
	sys_exit("");
}
© 2024 lite-C Forums