Одно примечание, которое я добавлю, который является от подобной проблемы на здесь, является этим, если Ваш Ajax возвращает следующее:
<div class="test">Hello</div>
<div class="one">World</div>
следующий jQuery не Будет работа:
$(data).find('div.test');
, поскольку отделения являются высокоуровневыми элементами, и данные не являются элементом, а строкой, чтобы заставить их работать необходимо использовать .filter
$(data).filter('div.test');
Лучше всего тестировать сами десятичные цифры, а не буквы. isdigit .
#include <ctype.h>
if(isdigit(variable))
{
//valid input
}
else
{
//invalid input
}
isalpha () будет проверять по одному символу за раз. Если пользователь вводит число вроде 23A4, вы хотите проверить каждую букву. Вы можете использовать это:
bool isNumber(char *input) {
for (i = 0; input[i] != '\0'; i++)
if (isalpha(input[i]))
return false;
return true;
}
// accept and check
scanf("%s", input); // where input is a pointer to a char with memory allocated
if (isNumber(input)) {
number = atoi(input);
// rest of the code
}
Я согласен с тем, что atoi () не является поточно-ориентированной и устаревшей функцией. Вместо этого вы можете написать другую простую функцию.
Помимо функции isalpha, это можно сделать так:
char vrbl;
if ((vrbl >= 'a' && vrbl <= 'z') || (vrbl >= 'A' && vrbl <= 'Z'))
{
printf("You entered a letter! You must enter a number!");
}
Здесь пригодятся библиотечные функции strto * ()
:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE ...
int main(void)
{
char buffer[SIZE];
printf("Gimme an integer value: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin))
{
long value;
char *check;
/**
* strtol() scans the string and converts it to the equivalent
* integer value. check will point to the first character
* in the buffer that isn't part of a valid integer constant;
* e.g., if you type in "12W", check will point to 'W'.
*
* If check points to something other than whitespace or a 0
* terminator, then the input string is not a valid integer.
*/
value = strtol(buffer, &check, 0);
if (!isspace(*check) && *check != 0)
{
printf("%s is not a valid integer\n", buffer);
}
}
return 0;
}
int strOnlyNumbers(char *str)
{
char current_character;
/* While current_character isn't null */
while(current_character = *str)
{
if(
(current_character < '0')
||
(current_character > '9')
)
{
return 0;
}
else
{
++str;
}
}
return 1;
}