2 registered members (AndrewAMD, juanex),
1,247
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
How to check the data entered in the text box and save?
#479119
02/20/20 08:05
02/20/20 08:05
|
Joined: Jan 2012
Posts: 108
gamers
OP
Member
|
OP
Member
Joined: Jan 2012
Posts: 108
|
Hi there, I need some help, again. How to save the result to the log file in desired conditions by checking the data entered in the textbox? For example, how can I ensure that the data is saved in the log file only when a value between 0 and 100 is entered in the text box, and that the user is warned when any data is entered or a character is written outside of mentioned values? In the example below, I was able to save the data entered in textbox, but I could not check the data type. I'll be happy if you can help me. Yours truly, Here is the example: https://we.tl/t-uS1ItW0oRb
|
|
|
Re: How to check the data entered in the text box and save?
[Re: 3run]
#479121
02/20/20 09:35
02/20/20 09:35
|
Joined: Jan 2012
Posts: 108
gamers
OP
Member
|
OP
Member
Joined: Jan 2012
Posts: 108
|
Hey,
So basically you only need to save input from user in range of 0...99 into the file, and everything different from that shouldn't be saved + user should be warned that entered data is incorrect?
Greets That's true, If I can check the data entered in the textbox, it can already save sample code, but I was not able to integrate the data control in to the sample code. Best regards,
|
|
|
Re: How to check the data entered in the text box and save?
[Re: gamers]
#479123
02/20/20 10:03
02/20/20 10:03
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Well, you needed to use stuff I've made in this thread.Try this code:
BMAP *pointer_tga = "pointer.png";
STRING *name_str = "Click to Enter Value Here!";
STRING *enter_value_str = "Click to Enter Value Here!";
STRING *error_msg_str = "You need to enter a number from 0 to 99!";
STRING *data_saved_msg_str = "Data was successfully saved into the log file.";
function input_name();
PANEL *input_pan =
{
layer = 10;
bmap = "textbox.png";
on_click = input_name;
flags = SHOW;
}
TEXT *name_txt =
{
layer = 11; // just above the textbox panel
font = "Calibri#30";
string(name_str);
flags = SHOW | CENTER_X | CENTER_Y;
}
// 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;
}
}
function input_name()
{
str_cpy(name_str, " ");
var input = inkey(name_str);
if(input != 13) // terminated input with ESC or anything else ?
{
// then return "Click to Enter Value Here!" message back
// same for all errors bellow !
str_cpy(name_str, enter_value_str);
}
else // pressed enter ?
{
// string is empty ? then show error message
if(is_empty_str(name_str))
{
error(error_msg_str);
str_cpy(name_str, enter_value_str);
}
else // if not ?
{
// if numeric ? then save the file
if(is_numeric_str(name_str))
{
// we can also check for the range here
// if it's more than 2 characters, then we are out of the range
if(str_len(name_str) > 2)
{
error(error_msg_str);
str_cpy(name_str, enter_value_str);
}
else // 0 ... 99
{
var filehandle;
filehandle = file_open_write("log.xml");
file_str_write(filehandle, name_str);
file_close(filehandle);
error(data_saved_msg_str);
}
}
else // not numeric ? then show error message
{
error(error_msg_str);
str_cpy(name_str, enter_value_str);
}
}
}
}
function mouse_startup()
{
warn_level = 6; // make sure to show all errors
fps_max = 60; // limit fps to 60
mouse_mode = 4; // this will take care of mouse_cursor/mouse_pos stuff
mouse_map = pointer_tga;
while(!key_esc)
{
// we need to do this in order to have proper panel positions with different screen resolutions
input_pan->pos_x = (screen_size.x / 2) - (bmap_width(input_pan->bmap) / 2);
input_pan->pos_y = (screen_size.y / 2) - (bmap_height(input_pan->bmap) / 2) - 64;
name_txt->pos_x = input_pan->pos_x + (bmap_width(input_pan->bmap) / 2);
name_txt->pos_y = input_pan->pos_y + (bmap_height(input_pan->bmap) / 2);
wait(1);
}
} Greets!
Last edited by 3run; 02/20/20 10:04.
|
|
|
Re: How to check the data entered in the text box and save?
[Re: 3run]
#479124
02/20/20 12:12
02/20/20 12:12
|
Joined: Jan 2012
Posts: 108
gamers
OP
Member
|
OP
Member
Joined: Jan 2012
Posts: 108
|
Well, you needed to use stuff I've made in this thread.Try this code:
BMAP *pointer_tga = "pointer.png";
STRING *name_str = "Click to Enter Value Here!";
STRING *enter_value_str = "Click to Enter Value Here!";
STRING *error_msg_str = "You need to enter a number from 0 to 99!";
STRING *data_saved_msg_str = "Data was successfully saved into the log file.";
function input_name();
PANEL *input_pan =
{
layer = 10;
bmap = "textbox.png";
on_click = input_name;
flags = SHOW;
}
TEXT *name_txt =
{
layer = 11; // just above the textbox panel
font = "Calibri#30";
string(name_str);
flags = SHOW | CENTER_X | CENTER_Y;
}
// 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;
}
}
function input_name()
{
str_cpy(name_str, " ");
var input = inkey(name_str);
if(input != 13) // terminated input with ESC or anything else ?
{
// then return "Click to Enter Value Here!" message back
// same for all errors bellow !
str_cpy(name_str, enter_value_str);
}
else // pressed enter ?
{
// string is empty ? then show error message
if(is_empty_str(name_str))
{
error(error_msg_str);
str_cpy(name_str, enter_value_str);
}
else // if not ?
{
// if numeric ? then save the file
if(is_numeric_str(name_str))
{
// we can also check for the range here
// if it's more than 2 characters, then we are out of the range
if(str_len(name_str) > 2)
{
error(error_msg_str);
str_cpy(name_str, enter_value_str);
}
else // 0 ... 99
{
var filehandle;
filehandle = file_open_write("log.xml");
file_str_write(filehandle, name_str);
file_close(filehandle);
error(data_saved_msg_str);
}
}
else // not numeric ? then show error message
{
error(error_msg_str);
str_cpy(name_str, enter_value_str);
}
}
}
}
function mouse_startup()
{
warn_level = 6; // make sure to show all errors
fps_max = 60; // limit fps to 60
mouse_mode = 4; // this will take care of mouse_cursor/mouse_pos stuff
mouse_map = pointer_tga;
while(!key_esc)
{
// we need to do this in order to have proper panel positions with different screen resolutions
input_pan->pos_x = (screen_size.x / 2) - (bmap_width(input_pan->bmap) / 2);
input_pan->pos_y = (screen_size.y / 2) - (bmap_height(input_pan->bmap) / 2) - 64;
name_txt->pos_x = input_pan->pos_x + (bmap_width(input_pan->bmap) / 2);
name_txt->pos_y = input_pan->pos_y + (bmap_height(input_pan->bmap) / 2);
wait(1);
}
} Greets! Thank you @3run That was great solution! This is exactly what I want to do.
|
|
|
Re: How to check the data entered in the text box and save?
[Re: 3run]
#479126
02/20/20 15:05
02/20/20 15:05
|
Joined: Jan 2012
Posts: 108
gamers
OP
Member
|
OP
Member
Joined: Jan 2012
Posts: 108
|
You are welcome! Glad to be helpful. Thank you again, @3run. I just want to ask because I'm curious about. If the user provided a condition between 0 and 150 (instead of 0 to 100 value), how could we do that?
|
|
|
Re: How to check the data entered in the text box and save?
[Re: gamers]
#479127
02/20/20 15:17
02/20/20 15:17
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
I would change 2 to 3 in this part
// we can also check for the range here
// if it's more than 2 characters, then we are out of the range
if(str_len(name_str) > 2)
So we will allow to enter value which is made out of 3 characters. Then I would convert string into the variable using str_to_num. And at the end I would check if this variable is higher than 150 or not. If it is, then give error, if it is not then save data. Best regards!
|
|
|
Re: How to check the data entered in the text box and save?
[Re: gamers]
#479129
02/20/20 17:57
02/20/20 17:57
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, I think that when you work with numbers is better to use numerical variables instead of strings. Here goes a simple implementation with the use of 'key_any' and 'key_lastpressed' function num_edit (var *_ptr_num, var _max) {
var _old_value = *_ptr_num;
var _last_pressed = key_lastpressed;
while(1) {
if(key_any) {
if(key_lastpressed != _last_pressed) {
_last_pressed = key_lastpressed;
switch(_last_pressed) {
case 1: // esc
*_ptr_num = _old_value;
return;
case 14: // back space
*_ptr_num = floor(*_ptr_num / 10);
break;
case 28: // enter
return;
default: // any other key
if(_last_pressed > 11) // from 1 to 0 keys -> from 2 to 11 scancodes
break;
var _res = *_ptr_num * 10 + (_last_pressed - 1) % 10; // 11 = scancode of 0 character
if(_res > _max)
break;
*_ptr_num = _res;
}
}
} else {
_last_pressed = 0;
}
wait(1);
}
}
// --------------------------------------------------
function main () {
static var my_num = 20;
num_edit(my_num, 150);
while(1) {
DEBUG_VAR(my_num, 100);
wait(1);
}
} Salud!
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|