JavaScript - Validation Function [RESOLVED]
I need to validate a text box in an ASP.NET project and am trying to use JavaScript even though I have never used it before. My task is so simply, I am embarrassed to ask; how do I ensure my string is at least 6 characters?
Here is my existing code:
Code:
<script language="javascript">
function PasswordCheck(source, arguments)
{
if string.length(arguments.value) > 5)
arguments.IsValid=true;
else
arguments.IsValid=false;
}
</script>
I get an "Object Required" error when it runs. :(
Re: JavaScript - Validation Function
NameOfString.length will give you the length of the string.
Re: JavaScript - Validation Function
I've changed my code accordingly:
Code:
<script language="javascript">
function PasswordCheck(source, arguments)
{
if arguments.value.length > 5
arguments.IsValid=true;
else
arguments.IsValid=false;
}
</script>
...but I still get "Object Expected" error...:(
Re: JavaScript - Validation Function
What is "source" and "arguments"?
Re: JavaScript - Validation Function
To clarify, I the function should be being called from the following validation control:
Code:
<asp:customvalidator
id="CustomValidator1"
runat="server"
ClientValidationFunction="PasswordCheck"
ControlToValidate="txtPassword"
Display="Dynamic"
ErrorMessage="The password must be at least 6 characters,">
</asp:customvalidator>
Re: JavaScript - Validation Function
try this
<script language="javascript">
function PasswordCheck(source, argumentsID)
{
var obj = document.getelementbyid(argumentsID)
if (obj)
{
if obj.value.length > 5
return true;
else
return false;
}
}
</script>
Re: JavaScript - Validation Function
I still don't see how source and argument are passed to that function.
Re: JavaScript - Validation Function
skv_noida
Thanks, but I can't get the function you suggested to pass the syntax check...:(
Mendhak
Quote:
I still don't see how source and argument are passed to that function.
All I can say is that I can get it working in vbScript. The validation functions are supposed to take those parameters as far as I can tell. However, I need to get this working in JavaScript...
Re: JavaScript - Validation Function
Doesn't anybody here know how to write validation functions for the CustomerValidator control in javascript here? I would have thought it were such a simple task, something that was common place...:confused:
Perhaps if anyone can tell me how to declare and instantiate a string object, assign it's value from arguments.value, that would probably do it...
Re: JavaScript - Validation Function
Your problem is 'value' is lowercase, the first letter should be capitalized in your script above... args.Value instead of args.value.
Here's a tip :
Type in 'CustomValidator' in google, and the first hit has a step-by-step on how to work with CustomValidators.
Here's another tip:
Next time you need help with Javascript strings, type 'Javascript Strings' into Google first...you'll get a much faster response - I guarantee it!
Re: JavaScript - Validation Function
Thankyou nemaroller! So Javascript's case sensitive, eh?