Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert a vb function to javascript function... 1

Status
Not open for further replies.

petersmith

Technical User
Sep 16, 2001
5
GB
Hi i have this working vb function which i need to call on an onclick event in javascript...


function ValKey(Vin)
dim k, i, s, b
k= ""

if len(Vin) = 7 then
s = UCase(mid(Vin,1,1))
if s = "A" or s = "S" or s = "M" then
b = true
for i = 2 to 7
if not IsNumeric(mid(Vin,i,1)) then b = false
next
if b then k = ucase(Vin)
end if
end if
Valkey = k
end function

can anyone show me how to convert it or get it to work on an onclick event?

kindest thanks.
 
Thanks for replying, just need it to work on an onclick event. How to i embed it in javascript?

regards,

Pete
 
How about this (using regular expressions):
Code:
<html><head><title></title>
<script type="text/javascript">
function valKey(_myKey){
  return (/[AaMmSs]{1}[0123456789]{7}/g.test(_myKey));
}
</script>
</head>
<body>
<form name="myTestForm" action="">
<input type="text" name="keyField" value="A1234567" onchange="alert(valKey(this.value))">
<input type="button" value="Test" onclick="alert(valKey(forms[0].elements['keyField'].value));">
</form>
</body>
</html>
I have added an onclick for a button and an onchange to the input element to allow you to test it.

Hope this does all you want [smile]
Jeff
 
Ah... looking at your code you wanted 6 digits... my code requires 7 digits. Make this change:
Code:
return (/[AaMmSs]{1}[0123456789]{[COLOR=red]6[/color]}/g.test(_myKey));
Cheers,
Jeff
 
Thanks, this works. How would you get it to display false if there are more than 6 digits and display an alert saying "incorect format" or somthing?

kindest thanks.
 
Hi i have tried to call this function on an onclick event but it doesn't work..


<script type="text/javascript">
function valKey(){
var Key

Key = document.form1.minvin.value;


if (/[AaMmSs]{1}[0123456789]{6}/g.test(Key)) == true {
document.form1.form.submit ;
else
alert("Vin format is incorrect (e.g. A123456)\nPlease try again.")
}
}
</script>

Any ideas where i have gone wrong?

kindest thanks.
 

Something like this will kind of do it:
Code:
<script type="text/javascript">
function valKey(_myKey){
  var bValid = /[AaMmSs]{1}[0123456789]{7}/g.test(_myKey);
  if (!bValid) errMsg += '\nIncorrect format. Use A, M, S followed by exactly 6 digits. Example: A123456';
  return bValid;
}
</script>

Cheers,
Jeff

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top