I have a specific question and a general one, about validation of fields when submitted in a form.
The specific question;
I'm trying to make a field where it's just possible to submit a sequence of 3 letters and then 3 digits in a row. Example: abc123. I guess the easiest way is using Javascript? Maybe by regular expressions? Unfortunately I'm not good enough at programming and can't code it myself..
Ok, the general question;
Is there any good source of different validation functions out there? More complex ones, like when the user must enter specific formats in a specific order. With and without special characters, etc. I've searched for it for hours, but I can't find any good ones.
Any help is much appreciated!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The forms are written in javascript and PHP. There are many sources for regular expression syntax, on the web, and published text.
You will likely never find an exact match to your particular requirement unless it is something like email validation but you will find lots of examples and syntax definitions that should help you write the code you need.
Your first problem might be solved in PHP like this:
preg_match("/[a-zA-Z]{3}[0-9]{3}/",$variabletochk,$result);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, thanks for your reply. Even though it was too advanced for me. I have no clue where to put that code to make it work.. And I also have to tie it to the field somehow.
Anyway, I tried to make the field only take numerical values with this Javascript code, but I cant get it to work. It doesn't validate any fields at all now. Any ideas?
Within head and javascript in form.html
function ValidateNumeric(fieldId, fieldBoxId, fieldType, required)
{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
alert("Du kan endast ange siffror");
}
}
return IsNumber;
}
Then I changed the name "ValidateField" to "ValidateNumeric" and "text" to "number".
if (ValidateNumeric('field_1','fieldBox_1','number',1) == false)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I can't make any sense out of your code, or the context you're using it in. You have random variables that are not defined, but are being used. Then you also have a for loop with an error check but no break, which would give you repeated alert messages.
Anyway, for your what you want is something like this:
I have a specific question and a general one, about validation of fields when submitted in a form.
The specific question;
I'm trying to make a field where it's just possible to submit a sequence of 3 letters and then 3 digits in a row. Example: abc123. I guess the easiest way is using Javascript? Maybe by regular expressions? Unfortunately I'm not good enough at programming and can't code it myself..
Ok, the general question;
Is there any good source of different validation functions out there? More complex ones, like when the user must enter specific formats in a specific order. With and without special characters, etc. I've searched for it for hours, but I can't find any good ones.
Any help is much appreciated!
The forms are written in javascript and PHP. There are many sources for regular expression syntax, on the web, and published text.
You will likely never find an exact match to your particular requirement unless it is something like email validation but you will find lots of examples and syntax definitions that should help you write the code you need.
Your first problem might be solved in PHP like this:
preg_match("/[a-zA-Z]{3}[0-9]{3}/",$variabletochk,$result);
Ok, thanks for your reply. Even though it was too advanced for me. I have no clue where to put that code to make it work.. And I also have to tie it to the field somehow.
Anyway, I tried to make the field only take numerical values with this Javascript code, but I cant get it to work. It doesn't validate any fields at all now. Any ideas?
Within head and javascript in form.html
function ValidateNumeric(fieldId, fieldBoxId, fieldType, required)
{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
alert("Du kan endast ange siffror");
}
}
return IsNumber;
}
Then I changed the name "ValidateField" to "ValidateNumeric" and "text" to "number".
if (ValidateNumeric('field_1','fieldBox_1','number',1) == false)
Have you solved this problem yet?
I can't make any sense out of your code, or the context you're using it in. You have random variables that are not defined, but are being used. Then you also have a for loop with an error check but no break, which would give you repeated alert messages.
Anyway, for your what you want is something like this:
function validateMyType(fieldID)
{
fieldObj = document.getElementById(fieldId);
return /^[a-zA-Z]{3}[0-9]{3}$/.test( fieldObj.value );
}
Thank you! This worked very well!
This makes the main error message to kick in if the field isn't properly filled in.
Yes, thank you very much! I used the javascript code.